Excellent video thanks so much , besides, Could I have one question, If I deploy service as window service, and some one stop service on service window when it running, how we get the information of stop time
Hi Depores, If you are inheriting BackgroundService for your Worker Service, you can override the public StopAsync method. This gets called when the Worker Service is stopped, which will happen when the Windows Service is stopped. If you're inheriting the IHostedService, you have to include the StopAsync declaration within your Worker Service, as it's a method within that interface.
Well you can certainly follow this tutorial to create a background service. For reading and writing files/directories, checkout this Microsoft documentation on how to do. It should have everything you need: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/
I came across an article that advised a good approach would be to pass an instance of IServiceScopeFactory through the constructor of a class when working with IHosted Services, then when I reach the a method that actually requires the service then create a scope using the scope factory. The explanation given in the article --> "If you are resolving a service in a method body, always create a child service scope to ensure that the resolved services are properly released. If a method gets IServiceProvider as an argument, then you can directly resolve services from it without care about releasing/disposing. Creating/managing service scope is a responsibility of the code calling your method. Following this principle makes your code cleaner. Do not hold a reference to a resolved service! Otherwise, it may cause memory leaks and you will access to a disposed service when you use the object reference later (unless the resolved service is singleton)." I can't see anything in MS Docs that talk about this though. In most IHosted Services, I usually put my business logic into seperate classes, so the StartAsync & StopAsync methods in the IHostedServoce class simply intiates the startup of some process in my other classes, and it's within those other seperate classes where i usually need to resolve a scoped service. Interested in your thoughts on this, thx
I forgot to add the final note, which concludes why this particular article i read suggested using IServiceScopeFactory. "The "IServiceProvider will give you only the root service provider. While it also implements the interface to create a scope, you could use it. But the general rule is to request as little as necessary so we are using the IScopeServiceFactory instead.
Thanks Oliver. The IServiceProvider.CreateScope() method gets an instance from the IoC Container for IServiceScopeFactory and calls the CreateScope method within it. As a result, calling it from IServiceScopeFactory is going to be more lightweight, because you are only getting one instance from the IoC Container rather than two. Unless you are calling other methods from the IServiceProvider instance, using IServiceScopeFactory is probably a better way to create a scope.
This explanation is miles better than the MS documentation - thanks for creating this!
Thanks Ed.
I like the structured introduction at the beginning. It help to understand the objectives and the implementation process
Thanks for your comment David.
Thank you for describing the differences between IHostedService and BackgroundService
Thanks for the comment, Aaron.
Nice job. The video is well structured and explained! I hope you can continue to share your knoledge. Thanks
Thanks Stefano.
There's more stuff coming!
Thank you! I have clarified some points about the while loop in BackgroundService. It's totally subscribe
Thanks Vadym
Great video. I was able to solve the issue I had thanks to this.
Thanks Falcon.
Nice demo. Thank you!
Thanks Sto Yan.
Excellent video thanks so much , besides, Could I have one question, If I deploy service as window service, and some one stop service on service window when it running, how we get the information of stop time
Hi Depores,
If you are inheriting BackgroundService for your Worker Service, you can override the public StopAsync method.
This gets called when the Worker Service is stopped, which will happen when the Windows Service is stopped.
If you're inheriting the IHostedService, you have to include the StopAsync declaration within your Worker Service, as it's a method within that interface.
Awesome, so simple and very well explained the complexity behind the scene :-)
Thanks Abdul.
Excellent video thank you!!
Thanks Merra
how to create a windows based service which will run as a background service and reads file from a folder and dumps into another
.please help
Well you can certainly follow this tutorial to create a background service.
For reading and writing files/directories, checkout this Microsoft documentation on how to do. It should have everything you need:
learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/
Smile Please, U are very serious -:)
:)
Typo:
They can be ran in an ASP.NET Core...
Should be:
They can be RUN in an ASP.NET Core...
Thanks Jim. Corrected.
I came across an article that advised a good approach would be to pass an instance of IServiceScopeFactory through the constructor of a class when working with IHosted Services, then when I reach the a method that actually requires the service then create a scope using the scope factory. The explanation given in the article --> "If you are resolving a service in a method body, always create a child service scope to ensure that the resolved services are properly released. If a method gets IServiceProvider as an argument, then you can directly resolve services from it without care about releasing/disposing. Creating/managing service scope is a responsibility of the code calling your method. Following this principle makes your code cleaner. Do not hold a reference to a resolved service! Otherwise, it may cause memory leaks and you will access to a disposed service when you use the object reference later (unless the resolved service is singleton)."
I can't see anything in MS Docs that talk about this though. In most IHosted Services, I usually put my business logic into seperate classes, so the StartAsync & StopAsync methods in the IHostedServoce class simply intiates the startup of some process in my other classes, and it's within those other seperate classes where i usually need to resolve a scoped service.
Interested in your thoughts on this, thx
I forgot to add the final note, which concludes why this particular article i read suggested using IServiceScopeFactory. "The "IServiceProvider will give you only the root service provider. While it also implements the interface to create a scope, you could use it. But the general rule is to request as little as necessary so we are using the IScopeServiceFactory instead.
Nice video by the way, very well explained, many thanks
Thanks Oliver.
The IServiceProvider.CreateScope() method gets an instance from the IoC Container for IServiceScopeFactory and calls the CreateScope method within it. As a result, calling it from IServiceScopeFactory is going to be more lightweight, because you are only getting one instance from the IoC Container rather than two.
Unless you are calling other methods from the IServiceProvider instance, using IServiceScopeFactory is probably a better way to create a scope.