.NET Core 3 introduced a new project template called Worker Service. This template is designed to give you a starting point for cross-platform services. As an alternate use case, it sets up a very nice environment for general console applications that is perfect for containers and microservices.
.NET Core 3引入了一个名为Worker Service的新项目模板。 该模板旨在为您提供跨平台服务的起点。 作为替代用例,它为通用控制台应用程序设置了一个非常好的环境,非常适合容器和微服务。
Some of the benefits of using this template include the following areas.
使用此模板的一些好处包括以下方面。
依赖注入 (Dependency Injection)
The Worker Service template configures a default Dependency injection container, ready for us to use. This is a huge benefit compared to the generic Console template.
Worker Service模板配置了一个默认的Dependency注入容器,可供我们使用。 与通用控制台模板相比,这是一个巨大的好处。
Adding Services involves updating the ConfigureServices method in the Program.cs file:
添加服务涉及更新Program.cs文件中的ConfigureServices方法:
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<ICustomerService,CustomerService>();
services.AddHostedService<Worker>();
});
组态 (Configuration)
The same configuration providers setup for ASP.NET Core are duplicated here for Worker Services. This gives us a powerful and familiar environment for storing configuration information:
这里为工人服务重复了ASP.NET Core的相同配置提供程序设置。 这为我们提供了一个强大而熟悉的环境来存储配置信息:
- appsettings.json appsettings.json
- appsettings.{Environment}.json appsettings。{Environment} .json
- User Secrets (for development only) 用户密钥(仅用于开发)
- Environment Variables 环境变量
- Command Line arguments 命令行参数
For additional information on each of the providers please see my previous article posted here.
有关每个提供程序的其他信息,请参阅此处发布的上一篇文章。
记录中 (Logging)
Likewise, logging providers have been configured to match the default setup for ASP.Net Core, giving you the following providers:
同样,日志记录提供程序已配置为与ASP.Net Core的默认设置匹配,从而为您提供以下提供程序:
- Console 安慰
- Debug 除错
- EventSource 事件源
- EventLog (only when running on Windows) EventLog(仅在Windows上运行时)
You can modify the logging providers by adding a ConfigureLogging method to the Host object in Program.cs:
您可以通过将ConfigureLogging方法添加到Program.cs中的Host对象来修改日志记录提供程序:
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
});
For additional information on logging, please see the documentation for ASP.NET Core.
有关日志记录的其他信息,请参见ASP.NET Core 文档 。
工人启动班 (Worker Startup Class)
Finally, the Worker.cs file is where the bulk of your code will exist. There are 3 overridable methods from the base class BackgroundService that let you tie into the lifecycle of your application:
最后, Worker.cs文件是您大部分代码所在的位置。 基类BackgroundService提供了3种可覆盖的方法,这些方法使您可以绑定到应用程序的生命周期:
ExecuteAsync – an abstract method used as the main entry point for your application. If this method exits, then your application shuts down.
ExecuteAsync –用作应用程序的主要入口点的抽象方法。 如果退出此方法,则您的应用程序将关闭。
StartAsync – A virtual method that, when overridden, is called when the service is starting, and can be used for one-time setup of resources.
StartAsync –一种虚拟方法,当覆盖该虚拟方法时,将在服务启动时调用该方法,该方法可用于一次性设置资源。
StopAsync – A virtual method that is called when the application is shutting down, and is a good place to release resources and dispose objects.
StopAsync –在应用程序关闭时调用的虚拟方法,是释放资源和处置对象的好地方。
摘要 (Summary)
The new worker service template in .NET Core 3 creates a hosting environment that is well-suited for console applications, microservices, containerized applications, and cross-platform background services. While these benefits can be configured independently of the template, the Worker Service template gives us a consistent startup environment between ASP.NET Core and Console applications.
.NET Core 3中的新工作程序服务模板创建了一个托管环境,非常适合控制台应用程序,微服务,容器化应用程序和跨平台后台服务。 尽管可以独立于模板配置这些好处,但是Worker Service模板为我们提供了ASP.NET Core和控制台应用程序之间一致的启动环境。