.NET Core Workers as Windows Services - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic, & .Net Frameworks (https://www.sickgaming.net/forum-79.html) +--- Thread: .NET Core Workers as Windows Services (/thread-89583.html) |
.NET Core Workers as Windows Services - xSicKxBot - 04-01-2019 .NET Core Workers as Windows Services <div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services.jpg" width="58" height="58" title="" alt="" /></div><div><div class="row justify-content-center"> <div class="col-md-2"> <div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services.jpg" width="58" height="58" alt="Avatar" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 photo avatar-default"></p> <p>Glenn</p> </div> </div> </div> <div class="entry-meta"> <p>March 29th, 2019</p> <p> <!–<span class="posted-on">Posted on <a href="https://devblogs.microsoft.com/aspnet/net-core-workers-as-windows-services/" rel="bookmark"><time class="entry-date published" datetime="2019-03-29T11:39:51+00:00">March 29, 2019</time><time class="updated" datetime="2019-03-29T11:47:44+00:00"> (March 29, 2019) </time></a></span><span class="byline"> by <span class="author vcard"><a class="url fn n" href="https://devblogs.microsoft.com/aspnet/author/glenncmicrosoft-com/">Glenn Condron [MSFT]</a></span></span>–> </div> <p><!-- .entry-meta --> </p> <p>In .NET Core 3.0 we are introducing a new type of application template called Worker Service. This template is intended to give you a starting point for writing long running services in .NET Core. In this walkthrough we will create a worker and run it as a Windows Service.</p> <h2>Create a worker</h2> <p><strong><em>Preview Note: In our preview releases the worker template is in the same menu as the Web templates. This will change in a future release. We intend to place the Worker Server template directly inside the create new project wizard.</em></strong></p> <h3>Create a Worker in Visual Studio</h3> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services.png" alt="image"></p> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-1.png" alt="image"></p> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-2.png" alt="image"></p> <h3>Create a Worker on the command line</h3> <p>Run <code>dotnet new worker</code></p> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-3.png" alt="image"></p> <h2>Run as a Windows Service</h2> <p>In order to run as a Windows Service we need our worker to listen for start and stop signals from <code>ServiceBase</code> the .NET type that exposes the Windows Service systems to .NET applications. To do this we want to:</p> <p>Add the <code>Microsoft.Extensions.Hosting.WindowsServices</code> NuGet package</p> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-4.png" alt="image"></p> <p>Add the <code>UseServiceBaseLifetime</code> call to the <code>HostBuilder</code> in our Program.cs</p> <pre><code class="csharp">public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceBaseLifetime() .ConfigureServices(services => { services.AddHostedService<Worker>(); }); } </code></pre> <p>This method does a couple of things. First, it checks whether or not the application is actually running as a Windows Service, if it isn’t then it noops which makes this method safe to be called when running locally or when running as a Windows Service. You don’t need to add guard clauses to it and can just run the app normally when not installed as a Windows Service.</p> <p>Secondly, it configures your host to use a <code>ServiceBaseLifetime</code>. <code>ServiceBaseLifetime</code> works with <code>ServiceBase</code> to help control the lifetime of your app when run as a Windows Service. This overrides the default <code>ConsoleLifetime</code> that handles signals like CTL+C.</p> <h3>Install the Worker</h3> <p>Once we have our worker using the <code>ServiceBaseLifetime</code> we then need to install it:</p> <p>First, lets publish the application. We will install the Windows Service in-place, meaning the exe will be locked whenever the service is running. The publish step is a nice way to make sure all the files I need to run the service are in one place and ready to be installed.</p> <pre><code>dotnet publish -o c:\code\workerpub </code></pre> <p>Then we can use the <a href="https://docs.microsoft.com/en-us/windows/desktop/services/controlling-a-service-using-sc">sc utility</a> in an admin command prompt</p> <pre><code>sc create workertest binPath=c:\code\workerpub\WorkerTest.exe </code></pre> <p>For example:</p> <p><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-5.png" alt="image"></p> <p><em><strong>Security note:</strong> This command has the service run as local system, which <strong>isn’t something you will generally want to do</strong>. Instead you should create a service account and run the windows service as that account. We will not talk about that here, but there is some documentation on the ASP.NET docs talking about it here: <a href="https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.2">https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.2</a></em></p> <h2>Logging</h2> <p>The logging system has an Event Log provider that can send log message directly to the Windows Event Log. To log to the event log you can add the <code>Microsoft.Extensions.Logging.EventLog</code> package and then modify your <code>Program.cs</code>:</p> <pre><code class="csharp">public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(loggerFactory => loggerFactory.AddEventLog()) .ConfigureServices(services => { services.AddHostedService<Worker>(); }); </code></pre> <p>In upcoming previews we plan to improve the experience of using Workers with Windows Services by:</p> <ol> <li>Rename UseWindowsServiceBaseLifetime to UseWindowsService</li> <li>Add automatic and improved integration with the Event Log when running as a Windows Service.</li> </ol> <p>We hope you try out this new template and want you to let us know how it goes, you can file any bugs or suggestions here: <a href="https://github.com/aspnet/AspNetCore/issues/new/choose">https://github.com/aspnet/AspNetCore/issues/new/choose</a></p> <div class="authorinfoarea"> <div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/03/net-core-workers-as-windows-services-1.jpg" width="96" height="96" alt="Avatar" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 photo avatar-default"></div> </p></div> </div> |