This article is a mirror article of machine translation, please click here to jump to the original article.

View: 6049|Reply: 4

[Source] ASP.NET Core (nineteen) uses BackgroundService to run background tasks

[Copy link]
Posted on 2023-5-20 22:51:51 | | | |
Requirements: A static log queue is defined in the project, and each request may put messages into the queue, and we need the website to open a background task to process the messages in this queue.

Review:

A service based on .NET Core on Linux
https://www.itsvse.com/thread-9447-1-1.html
IHostedService interface

The IHostedService interface defines two methods for hosted objects:

StartAsync(CancellationToken): The logic used to start background tasks.
StopAsync(CancellationToken): Triggered when the host performs a normal shutdown, containing the logic to end background tasks.

BackgroundService base class

BackgroundService is the base class used to implement long-running IHostedService, the source code address:The hyperlink login is visible.



Call ExecuteAsync(CancellationToken) to run the background service. The implementation returns a Task that represents the entire lifetime of the background service. No other services are started until ExecuteAsync becomes asynchronous, such as by calling await. Avoid performing long blocking initialization tasks in ExecuteAsync. StopAsync(CancellationToken) waiting for ExecuteAsync to complete.

We implement a background task to print the log in a loop, the code is as follows:

Register in the service service with the following code:

The registration mode is a singleton, as shown in the figure below:



Start the project with the output as follows:



Use a scoped service in a background task

To use a scoped service in the BackgroundService, create a scope. By default, no scopes are created for managed services. If you use a constructor directly to inject a scoped object, the error is as follows:

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: WebApplication2.Worker.TestServiceHostedService': Cannot consume scoped service 'WebApplication2.Worker.User' from singleton ' Microsoft.Extensions.Hosting.IHostedService'.)
We need to modify the code as follows:

Reference:The hyperlink login is visible.

(End)





Previous:OData – OData vs GraphQL
Next:LiteDB Embedded Database for .NET tutorial
 Landlord| Posted on 2023-5-20 22:52:45 |


review

ASP.NET Core (18) Customize a simple OutputCache output cache
https://www.itsvse.com/thread-10583-1-1.html

ASP.NET Core (17) integrates MiniProfile application performance analysis
https://www.itsvse.com/thread-10571-1-1.html

ASP.NET Core (16) Dependent on injection of dynamic registration services
https://www.itsvse.com/thread-10560-1-1.html

ASP.NET Core (XV) uses HttpClient to send HTTP requests
https://www.itsvse.com/thread-10311-1-1.html

ASP.NET Core (fourteen) is based on the SkiaSharp image captcha
https://www.itsvse.com/thread-10287-1-1.html

ASP.NET Core (XIII) to determine whether it is an Ajax request or not
https://www.itsvse.com/thread-10284-1-1.html

ASP.NET Core (twelve) front-end JS, CSS bundling, and compression
https://www.itsvse.com/thread-10282-1-1.html

ASP.NET Core (XI) endpoint route adds middleware to display all DI services
https://www.itsvse.com/thread-10269-1-1.html

ASP.NET Detailed explanation of Configuration priorities in Core(10).
https://www.itsvse.com/thread-10265-1-1.html

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
 Landlord| Posted on 2023-5-29 20:29:45 |
You can replace IServiceProvider with IServiceScopeFactory, and there is no difference between the two when creating CreateScope.
 Landlord| Posted on 2023-8-2 19:13:00 |
A new timer in .NET 6, PeriodicTimer, is used
https://www.itsvse.com/thread-10637-1-1.html
 Landlord| Posted on 2023-8-13 20:13:37 |
StartAsync should be limited to Short-term missions, because the hosting service isRun in ordertargetNo other services are started until StartAsync is running completelyLong-term tasks should be placed in ExecuteAsync

Reference:The hyperlink login is visible.

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com