using SlnMesnac.Config; using SlnMesnac.Repository.service; using System; 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 保留所有权利。 * CLR版本:4.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; private readonly AppConfig _appConfig; private IReal_workdataService _real_WorkdataService; public WebApiServer(ApiServer apiServer, AppConfig appConfig, IReal_workdataService real_WorkdataService) { _apiServer = apiServer; _real_WorkdataService = real_WorkdataService; _appConfig = appConfig; } public void Init() { try { //获取汇集软件服务端口号 string port = _real_WorkdataService.GetPort(_appConfig.StationCode); var service = new HttpService(); service.SetupAsync(new TouchSocketConfig() .SetListenIPHosts(int.Parse(port)) .ConfigureContainer(a => { a.AddRpcStore(store => { store.RegisterServer(_apiServer);//注册服务 }); a.AddCors(corsOption => { corsOption.Add("cors", corsBuilder => { corsBuilder.AllowAnyMethod() .AllowAnyOrigin(); }); }); a.AddLogger(logger => { logger.AddConsoleLogger(); logger.AddFileLogger(); }); }) .ConfigurePlugins(a => { //a.UseTcpSessionCheckClear(options => //{ // options.CheckClearType = CheckClearType.All; // options.Tick = TimeSpan.FromSeconds(60); // options.OnClose = async (c, t) => // { // await c.CloseAsync("超时无数据"); // }; //}); a.Add(); a.UseWebApi() ; a.UseSwagger();//使用Swagger页面 //.UseLaunchBrowser(); a.UseDefaultHttpServicePlugin(); })); service.StartAsync(); 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(HttpSessionClient client, HttpContextEventArgs e) { await e.InvokeNext(); } public Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e) { throw new NotImplementedException(); } } }