.NET Hosted Service: Run a background service and task (uses .NET Core)

Поділитися
Вставка
  • Опубліковано 31 жов 2024

КОМЕНТАРІ • 30

  • @edw9805
    @edw9805 2 роки тому +3

    This explanation is miles better than the MS documentation - thanks for creating this!

  • @davidemmanuel3001
    @davidemmanuel3001 2 роки тому +7

    I like the structured introduction at the beginning. It help to understand the objectives and the implementation process

  • @aarontheeranitpongtongmuan233
    @aarontheeranitpongtongmuan233 2 роки тому +1

    Thank you for describing the differences between IHostedService and BackgroundService

  • @stefanomattiatroisi2018
    @stefanomattiatroisi2018 Рік тому +1

    Nice job. The video is well structured and explained! I hope you can continue to share your knoledge. Thanks

    • @RoundTheCode
      @RoundTheCode  Рік тому

      Thanks Stefano.
      There's more stuff coming!

  • @vadymkurinnyi310
    @vadymkurinnyi310 2 роки тому +1

    Thank you! I have clarified some points about the while loop in BackgroundService. It's totally subscribe

  • @FalconStakepool
    @FalconStakepool 2 роки тому +1

    Great video. I was able to solve the issue I had thanks to this.

  • @stoyan8338
    @stoyan8338 2 роки тому +1

    Nice demo. Thank you!

  • @deporesmartino1472
    @deporesmartino1472 2 роки тому +1

    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

    • @RoundTheCode
      @RoundTheCode  2 роки тому

      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.

  • @4Siblings313
    @4Siblings313 Рік тому

    Awesome, so simple and very well explained the complexity behind the scene :-)

  • @merrakokebie2564
    @merrakokebie2564 2 роки тому +1

    Excellent video thank you!!

  • @swapnilnayak919
    @swapnilnayak919 2 роки тому

    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

    • @RoundTheCode
      @RoundTheCode  2 роки тому

      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/

  • @arunbm123
    @arunbm123 Рік тому +2

    Smile Please, U are very serious -:)

  • @chezchezchezchez
    @chezchezchezchez 2 роки тому +1

    Typo:
    They can be ran in an ASP.NET Core...
    Should be:
    They can be RUN in an ASP.NET Core...

  • @gagwithgaffer8385
    @gagwithgaffer8385 2 роки тому

    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

    • @gagwithgaffer8385
      @gagwithgaffer8385 2 роки тому

      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.

    • @gagwithgaffer8385
      @gagwithgaffer8385 2 роки тому

      Nice video by the way, very well explained, many thanks

    • @RoundTheCode
      @RoundTheCode  2 роки тому

      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.