With .NET Core 3.0, you can create long running services as background services. With .NET Core 3.0.100-preview3, a new template is introduced named Worker Service. These services can be used to do things like processing messages in a queue, as a watcher etc.
In this post, let's see how we can get started on creating a Worker Service. For this you will need to have Visual Studio 2019 and .NET Core 3.0.100-preview3 installed in your machine. Remember to use .NET Core 3.0 with Visual Studio, you'll anyway need Visual Studio 2019 (By the way, Visual Studio 2019 RTM is getting released today, which I am very excited about).
Once those are installed, you can create a new ASP.NET Core Web Application. If you are wondering why do you need to create a Web Application, the reason is, currently the Worker template is sitting inside Web Application template and this is going to get changed in the future. The Worker Service template will be available directly inside the create new project wizard.
After giving the solution and project name, in the next dialog, you can see the Worker Service template. Make sure you have selected .NET Core and ASP.NET Core 3.0 as the target framework.
When the project is created, you can see two classes are created, first one is the familiar Program.
Program.cs
This is similar to what we used to in ASP.NET Core Web Applications, instead it directly configures services adding a service of any type which implements IHostedService. Here it's a type of a Worker.
Worker.cs
Worker implementing an abstract class BackgroundService which in turn implements IHostedService. Now if you run this, the message will get displayed until we issue an cancellation token, for instance until you press Ctrl + C etc.
Now you can use this to create a Windows Service or to use as a Linux Daemon with some very little changes.
Hope this helps.
Happy Coding.
Regards,
Jaliya
In this post, let's see how we can get started on creating a Worker Service. For this you will need to have Visual Studio 2019 and .NET Core 3.0.100-preview3 installed in your machine. Remember to use .NET Core 3.0 with Visual Studio, you'll anyway need Visual Studio 2019 (By the way, Visual Studio 2019 RTM is getting released today, which I am very excited about).
Once those are installed, you can create a new ASP.NET Core Web Application. If you are wondering why do you need to create a Web Application, the reason is, currently the Worker template is sitting inside Web Application template and this is going to get changed in the future. The Worker Service template will be available directly inside the create new project wizard.
ASP.NET Core Web Application |
Configure your new project |
Worker Service Template |
Program.cs
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices(services => { services.AddHostedService<Worker>(); }); }
Worker.cs
public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger) { _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation($"Worker running at: {DateTime.Now}"); await Task.Delay(1000, stoppingToken); } } }
Now you can use this to create a Windows Service or to use as a Linux Daemon with some very little changes.
Hope this helps.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment