{"id":38012,"date":"2019-12-11T10:05:30","date_gmt":"2019-12-11T17:05:30","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/premier-developer\/?p=38012"},"modified":"2020-01-09T06:45:52","modified_gmt":"2020-01-09T13:45:52","slug":"demystifying-the-new-net-core-3-worker-service","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/demystifying-the-new-net-core-3-worker-service\/","title":{"rendered":"Demystifying the new .NET Core 3 Worker Service"},"content":{"rendered":"<p>Premier Developer Consultant <a href=\"https:\/\/www.linkedin.com\/in\/randyrpatterson\/\">Randy Patterson<\/a> discusses the benefits of using the new Worker Service project template introduced in .NET Core 3.<\/p>\n<hr \/>\n<p>.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.<\/p>\n<p><img decoding=\"async\" width=\"1024\" height=\"710\" class=\"wp-image-38013\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/12\/word-image.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/12\/word-image.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/12\/word-image-300x208.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/12\/word-image-768x533.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Some of the benefits of using this template include the following areas.<\/p>\n<h2>Dependency Injection<\/h2>\n<p>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.<\/p>\n<p>Adding Services involves updating the <em>ConfigureServices<\/em> method in the <em>Program.cs<\/em> file:<\/p>\n<pre class=\"lang:default decode:true\">Host.CreateDefaultBuilder(args)\r\n   .ConfigureServices((hostContext, services) =&gt;\r\n   {\r\n      services.AddTransient&lt;ICustomerService,CustomerService&gt;();\r\n      services.AddHostedService&lt;Worker&gt;();\r\n   });<\/pre>\n<p>&nbsp;<\/p>\n<h2>Configuration<\/h2>\n<p>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:<\/p>\n<ol>\n<li>appsettings.json<\/li>\n<li>appsettings.{Environment}.json<\/li>\n<li>User Secrets (for development only)<\/li>\n<li>Environment Variables<\/li>\n<li>Command Line arguments<\/li>\n<\/ol>\n<p>For additional information on each of the providers please see my previous article posted <a href=\"https:\/\/devblogs.microsoft.com\/premier-developer\/order-of-precedence-when-configuring-asp-net-core\/\">here<\/a>.<\/p>\n<h2>Logging<\/h2>\n<p>Likewise, logging providers have been configured to match the default setup for ASP.Net Core, giving you the following providers:<\/p>\n<ol>\n<li>Console<\/li>\n<li>Debug<\/li>\n<li>EventSource<\/li>\n<li>EventLog (only when running on Windows)<\/li>\n<\/ol>\n<p>You can modify the logging providers by adding a <em>ConfigureLogging<\/em> method to the <em>Host <\/em>object in <em>Program.cs<\/em>:<\/p>\n<pre class=\"lang:default decode:true\">Host.CreateDefaultBuilder(args)\r\n   .ConfigureServices((hostContext, services) =&gt;\r\n   {\r\n      services.AddHostedService&lt;Worker&gt;();\r\n   })\r\n   .ConfigureLogging(logging =&gt;\r\n   {\r\n      logging.ClearProviders();\r\n      logging.AddConsole();\r\n   });<\/pre>\n<p>For additional information on logging, please see the <a href=\"https:\/\/docs.microsoft.com\/aspnet\/core\/fundamentals\/logging\/?view=aspnetcore-3.0\">documentation<\/a> for ASP.NET Core.<\/p>\n<h2>Worker Startup Class<\/h2>\n<p>Finally, the <em>Worker.cs<\/em> file is where the bulk of your code will exist. There are 3 overridable methods from the base class <em>BackgroundService<\/em> that let you tie into the lifecycle of your application:<\/p>\n<p><strong>ExecuteAsync<\/strong> \u2013 an abstract method used as the main entry point for your application. If this method exits, then your application shuts down.<\/p>\n<p><strong>StartAsync<\/strong> \u2013 A virtual method that, when overridden, is called when the service is starting, and can be used for one-time setup of resources.<\/p>\n<p><strong>StopAsync<\/strong> \u2013 A virtual method that is called when the application is shutting down, and is a good place to release resources and dispose objects.<\/p>\n<h2>Summary<\/h2>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET Core 3 introduced a new project template called a 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 perfect for containers and microservices.<\/p>\n","protected":false},"author":582,"featured_media":38022,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[80,1],"tags":[83,62,3],"class_list":["post-38012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-permierdev","tag-net-core","tag-microservices","tag-team"],"acf":[],"blog_post_summary":"<p>.NET Core 3 introduced a new project template called a 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 perfect for containers and microservices.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=38012"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38012\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/38022"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=38012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=38012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=38012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}