Now within this project, create a new file called ImplementIHostedService.cs. You can create a new project in Visual Studio to get started. Let's create our service using IHostedServiceįor the sake of simplicity, I am using the ASP.NET Core WebAPI project. You can register the hosted service in the application by calling the method: AddHostedService(). Next, this hosted service must be registered in the application during the startup of the application. Once implemented, the implementing class has to override two methods: StartAsync(CancellationToken) and StopAsync(CancellationToken). For any class to operate as a background service, it must implement this interface. The IHostedService is an interface that provides the base for implementing the background services or tasks. We will be creating a service that continuously prints out time after every second until the application shuts down. Here in this tutorial, we will stick to the solutions provided by the. You can use the Hangfire library or opt for some cloud based solution. NET Core offers three classes to assist you with it: IHostedService and BackgroundService. Now that you are equipped about the background tasks, let's look at the options available to you to build them. You want to periodically perform house keeping operations such as database cleaning, updating secondary database or garbage collections operations. Hence having background service taking care of this extensive task provides a smoother user experience. Reading lengthy files, which can be from gigabytes to terabytes, if we perform this operation on the main thread, it will freeze the application resulting in a downgraded user experience. You have to perform IO operations that are extensive such as reading the disk for files. Hence we can initiate this process in the background and let it listen. So this process runs throughout the lifetime of the application. For example, you might have an application that reacts to the change in the forex rates. This event can be either internal or external. You have to continuously listen for some event. Background jobs offer a perfect solution for this. For example, you might need to send emails to your subscribers at midnight. You might want to schedule some jobs for some specific time of day or at any particular time of day. Image processing and image manipulation, for instance, is a resource extensive task hence it is more suited for background operation. You have to process images or manipulation of media. This thread accepts the requests, performs logic, and finally returns the output.īut there are scenarios where you don't want to delegate your jobs to this main thread. In any application, there is a main thread that is responsible for the execution of the application. NET Core is Microsoft's offering for building cross-platform applications and now its gaining popularity more than ever.īut first, let's understand more about background services below: A Short Introduction to Background Servicesįirst, let us discuss why do we need background tasks. In this tutorial, we will be learning how to create background services in the. So how to handle such scenarios? An efficient way to tackle such scenarios is by using background services. If you are building a web application, you may have encountered scenarios where some operation needs to be performed indefinitely. Getting Started with Background Tasks In ASP.NET Core WebAPI
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |