windows 服务实现WebApi HttpSelfHostServer

您所在的位置:网站首页 winform调用webapi windows 服务实现WebApi HttpSelfHostServer

windows 服务实现WebApi HttpSelfHostServer

2023-05-23 00:01| 来源: 网络整理| 查看: 265

windows 服务实现WebApi HttpSelfHostServer 创建一个Windows服务 在这里插入图片描述 (一)构建服务

#region Web Api监听 Assembly.Load("WindowsSelfServer");//("Lind.DDD.TestApi"); //手工加载某个api程序集的controller var config = new HttpSelfHostConfiguration("http://localhost:3333"); config.Routes.MapHttpRoute("default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); var server = new HttpSelfHostServer(config); server.OpenAsync().Wait(); Console.WriteLine("Server is opened"); #endregion';

(二)Controller代码

/// /// 测试webapi /// public class TestController : ApiController { // GET api/ public IEnumerable Get() { return new string[] { "value1", "value2" }; } // GET api//5 public string Get(int id) { return "value"; } // POST api/ public void Post([FromBody]Demo value) { Thread.Sleep(10000); Logger.Core.LoggerFactory.Instance.Logger_Info(value.ToString()); } // PUT api//5 public void Put(int id, [FromBody]string value) { } // DELETE api//5 public void Delete(int id) { } } public class Demo { public string appName { get; set; } public string url { get; set; } public override string ToString() { return string.Format("appName:{0},url:{1},datetime:{2}", this.appName, this.url, DateTime.Now); } }

(三)启动需要注册到windows服务

1.以管理员身份运行cmd

2.安装windows服务

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319(InstallUtil.exe的路径,注意InstallUtil.exe的版本号需要和项目的版本号相同)

3.安装windows服务

InstallUtil.exe D:\项目相关文件\MonitoringTEST\DialTest\bin\Debug\ProjectTest.exe(项目的路径)

4.启动windows服务

net start TestServive(服务名称)

一开始我不太敢操作,但是遇到问题我还是决定试一下,首先在网上找了上面的操作步骤,但是不理解(从第二步开始)

注意注意InstallUtil.exe的版本号需要和项目的版本号相同

window服务需要做一个配置https://blog.csdn.net/taoerchun/article/details/92804492



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3