You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB
C#

2 months ago
using SlnMesnac.Config;
using SlnMesnac.Repository.service;
using System;
2 years ago
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi.Swagger;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* SlnMesnac.TouchSocket
* 4e47989b-9d43-426e-b67a-529de3b1b0e8
*
* WenJY
*
* 2024-09-04 10:51:29
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket
{
public class WebApiServer
{
private ApiServer _apiServer;
2 months ago
private readonly AppConfig _appConfig;
private IReal_workdataService _real_WorkdataService;
public WebApiServer(ApiServer apiServer, AppConfig appConfig, IReal_workdataService real_WorkdataService)
2 years ago
{
_apiServer = apiServer;
2 months ago
_real_WorkdataService = real_WorkdataService;
_appConfig = appConfig;
2 years ago
}
public void Init()
{
try
{
2 months ago
//获取汇集软件服务端口号
string port = _real_WorkdataService.GetPort(_appConfig.StationCode);
2 years ago
var service = new HttpService();
service.Setup(new TouchSocketConfig()
2 months ago
.SetListenIPHosts(int.Parse(port))
2 years ago
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<ApiServer>(_apiServer);//注册服务
});
a.AddCors(corsOption =>
{
corsOption.Add("cors", corsBuilder =>
{
corsBuilder.AllowAnyMethod()
.AllowAnyOrigin();
});
});
a.AddLogger(logger =>
{
logger.AddConsoleLogger();
logger.AddFileLogger();
});
})
.ConfigurePlugins(a =>
{
a.UseCheckClear();
a.Add<AuthenticationPlugin>();
a.UseWebApi()
.ConfigureConverter(converter =>
{
converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
});
a.UseSwagger();//使用Swagger页面
//.UseLaunchBrowser();
a.UseDefaultHttpServicePlugin();
}));
service.Start();
Console.WriteLine("以下连接用于测试webApi");
Console.WriteLine($"使用http://127.0.0.1:7789/swagger/index.html");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//Console.ReadLine();
}
}
internal class AuthenticationPlugin : PluginBase, IHttpPlugin
{
public async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e)
{
await e.InvokeNext();
}
}
2 years ago
}