设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 系统 > 正文

使用.NET Core创建Windows服务(一) - 使用官方推荐方式(2)

发布时间:2021-01-07 09:26 所属栏目:52 来源:网络整理
导读:但是至少我们可以做如下了事情了。 static void Main(string[] args){ var loggingService = new LoggingService(); if (true) //Some check to see if we are in debug mode (Either #IF Debug etc or an app sett

但是至少我们可以做如下了事情了。

static void Main(string[] args)
{
    var loggingService = new LoggingService();
    if (true)   //Some check to see if we are in debug mode (Either #IF Debug etc or an app setting)
    {
        loggingService.OnStartPublic(new string[0]);
        while(true)
        {
            //Just spin wait here. 
            Thread.Sleep(1000);
        }
        //Call stop here etc. 
    }
    else
    {
        ServiceBase.Run(new LoggingService());
    }
}

你的另一个选择是,在调试模式下进行项目发布,安装服务,然后附加调试器。实际上,这是Microsoft建议你使用的方式,但是我认为这简直一团糟。

后续

实际上,我们可以在这里做一些其他非常有用的事情, 比如我们可以通过创建一个install.bat批处理文件来为我们运行SC Create命令。但我认为,上面我们看到的调试问题,已经让我不再想使用这种方式了。 幸运的是,有一个名为Topshelf的库可以帮助我们减轻很多麻烦,在本系列的下一部分中,我们将研究如何它。

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读