Topshelf 搭建輕量級 Windows 服務 + Webapi
創建控制臺項目
static void Main(string[] args) { var rc = HostFactory.Run(x => { x.Service<AppServices>(s => { s.ConstructUsing(name => new AppServices()); s.WhenStarted(service => service.Start()); s.WhenStopped(service => service.Stop()); s.OwinEndpoint(api => { api.Port = 9000; api.ConfigureHttp(httpConfiguration => { httpConfiguration.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{action}/{id}", new { action = RouteParameter.Optional, id = RouteParameter.Optional }); }); api.ConfigureAppBuilder(appBuilder => appBuilder.Use<CorsMiddleware>(CorsOptions.AllowAll)); }); }); x.RunAsLocalSystem(); //x.UseLog4Net("log4net.config"); x.SetDescription("DNC對接接口"); x.SetDisplayName("DNC對接接口"); x.SetServiceName("DNC.WebApi"); x.StartAutomaticallyDelayed(); }); log4net.Config.XmlConfigurator.Configure(); var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); Environment.ExitCode = exitCode; }
添加xxxApiController類
public class DncController : ApiController { [HttpPost] public string Send([FromBody]DownFileInputDto input) { return "hi"; } }
安裝服務:
以管理員身份運行cmd,執行 DNC-MES-Interface.exe install 命令安裝接口,執行 net start DNC.WebApi 命令啟用服務,接口運行在9000端口
Newd
版權聲明
作者:扶我起來我還要敲
地址:http://www.rzrgm.cn/Newd/p/12922485.html
? Newd 尊重知識產權,引用請注出處
廣告位
(虛位以待,如有需要請私信)
浙公網安備 33010602011771號