【问题标题】:Writing windows service in .net core在 .net core 中编写 windows 服务
【发布时间】:2016-11-23 11:58:01
【问题描述】:

我的问题是如何在以前的 .net 版本中编写的 .net core 中编写 windows 服务?

许多链接/文章解释了如何将您的 .net 核心应用程序托管为 Windows 服务。那么这是我创建 Windows 服务的唯一方法吗?

如果是,谁能提供它的链接/示例

谢谢!

【问题讨论】:

标签: c# asp.net-core windows-services .net-core


【解决方案1】:

这是在 .net Core(控制台应用)中构建 windows 服务的另一种简单方法

DotNetCore.WindowsService

允许将点网核心应用程序作为 Windows 服务托管的简单库。

安装

使用 nuget: 安装包 PeterKottas.DotNetCore.WindowsService

用法

  1. 创建 .NETCore 控制台应用程序。
  2. 创建您的第一个服务,如下所示:

    public class ExampleService : IMicroService
    {
        public void Start()
        {
            Console.WriteLine("I started");
        }
    
        public void Stop()
        {
            Console.WriteLine("I stopped");
        }
    }  
    
  3. 服务 API:

    ServiceRunner<ExampleService>.Run(config =>
    {
        var name = config.GetDefaultName();
        config.Service(serviceConfig =>
        {
            serviceConfig.ServiceFactory((extraArguments) =>
        {
            return new ExampleService();
        });
        serviceConfig.OnStart((service, extraArguments) =>
        {
            Console.WriteLine("Service {0} started", name);
            service.Start();
        });
    
        serviceConfig.OnStop(service =>
        {
            Console.WriteLine("Service {0} stopped", name);
            service.Stop();
        });
    
        serviceConfig.OnError(e =>
        {
            Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);});
        });
    });
    
  4. 运行不带参数的服务,它像控制台应用程序一样运行。

  5. 使用 action:install 运行该服务,它将安装该服务。
  6. 使用 action:uninstall 运行该服务,它将卸载该服务。
  7. 使用 action:start 运行服务,它将启动服务。
  8. 使用 action:stop 运行服务,它会停止服务。

【讨论】:

  • 在 .net core 2.0 中测试
  • 我可以把你用来测试 .net core 2.0 的项目给我吗?
  • @HaseebJadoon 当然,我会把它放在 GitHub 上并为你发布链接(可能在这里,在下一条评论中);)
  • Sample Service 基于主存储库示例。我希望它会有所帮助;)
  • 也与 Nano Server 一起运行
【解决方案2】:

不确定是否有这样做的默认方式。但是,您可以让您的核心应用程序继承自 ServiceBase 并实现必要的覆盖。我们在我们的应用程序中执行此操作(请注意,我们针对的是完整框架,而不是核心)。最初的想法来自以下文章:

请注意,这些文章仍然引用 DNX(从 .NET Core 测试版开始) - 现在您的核心应用程序将编译为 exe,因此您可以调整他们的示例以适应这种情况。

【讨论】:

  • 我读到微软的WebHostService类还有另一个类,哪个类适合使用ServiceBaseWebHostService类?
  • 似乎 WebHostService 是一种新的实现方式(我从help 看到它确实继承自ServiceBase)。根据this的问题,它是在RC2中添加的。还有一个很好的例子来说明如何使用它。另请查看this 文章。
【解决方案3】:

This is 一种使用 .net 核心构建 Windows 服务的方法。

它是使用对本地 Windows 程序集的 P/Invoke 调用构建的。

windows服务编写示例(取自github项目页面):

using DasMulli.Win32.ServiceUtils;

class Program
{
    public static void Main(string[] args)
    {
        var myService = new MyService();
        var serviceHost = new Win32ServiceHost(myService);
        serviceHost.Run();
    }
}

class MyService : IWin32Service
{
    public string ServiceName => "Test Service";

    public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)
    {
        // Start coolness and return
    }

    public void Stop()
    {
        // shut it down again
    }
}

它是not perfect,但恕我直言,它非常好。

【讨论】:

  • 这看起来棒极了
  • 如何从 Microsoft.AspNetCore.Mvc 运行控制器?
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 2021-11-17
  • 2019-12-20
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
相关资源
最近更新 更多