.NET Worker Service: Create a background service in C# (run as a Windows Service)

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

КОМЕНТАРІ • 84

  • @JanManipol
    @JanManipol 5 днів тому

    Thanks for the help. You're a savior.

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

    Thanks for the help. This worked beautifully. Better than the Microsoft Tutorial.

  • @stilllearningknowledge4529
    @stilllearningknowledge4529 2 місяці тому

    Thank you. Clear and simple. Nice explanation. Keep going

  • @adeni4359
    @adeni4359 2 роки тому +2

    Love your channel. Please more long tutorials!

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

    Thanks for info, it allowed me to created my service. Special thanks for subs ;)
    Keep up the good work!

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

    Thank you! Clear and simple

  • @aniljain50
    @aniljain50 2 роки тому +4

    Simple and excellent tutorial

  • @user-ib4sr6tr8m
    @user-ib4sr6tr8m Рік тому

    thank you so much man, for your videos, for the time and the love that you put in them, you are pretty helpful for me

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

      Thanks Camilo. Glad you are enjoying the content.

  • @jigspatel-nu3ee
    @jigspatel-nu3ee 3 місяці тому

    Thank you it's working, explained very easy

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

    Most Usefull Tutorial. Thank You!!

  • @gahshunker
    @gahshunker 2 місяці тому

    how can this made cross platform? any nuget package to have a host that is not windows specific? great video btw, concise and to the point. thanks

  • @duongchinhngu2407
    @duongchinhngu2407 9 місяців тому +1

    Thank you so much, your video saved my day!

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

    Thank you for the tutorial! I wasn't adding in the Microsoft.Extensions.Hosting.WindowsServices to my project and I couldn't figure out why my service was failing to start. It would run and log for 30 seconds and then quit with a message saying "The service did not respond to the start or control request in a timely fashion."

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

      Yes it can be hard to debug Windows Service's. Glad you managed to resolve the issue.

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

      When I run the consul, information is written in the logger
      ​ @Round The Code

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

    Is there a follow up video on this topic?

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

      Hi Hassan,
      What follow up video are you expecting?

  • @PremKumar-kg5ep
    @PremKumar-kg5ep Рік тому +1

    Very good explanations.

  • @pillockmacpillock2463
    @pillockmacpillock2463 3 місяці тому

    Not sure I like this implementation. Where are the classic start and stop stubs you get in framework? Feels like the core version is a bit noddy

    • @RoundTheCode
      @RoundTheCode  2 місяці тому

      Are you talking about the WorkerService?
      It inherits the IHostedService interface which includes StartAsync and StopAsync methods. These can be overloaded through the WorkerService.
      This video has more details about it:
      ua-cam.com/video/wfyqKXMfZHk/v-deo.html

  • @shreyasmahajan2157
    @shreyasmahajan2157 3 місяці тому

    In my case everything worked perfectly as demonstrated except the deployment part.
    After creating the service using "sc" command, I am getting the following error :
    Windows Could not start the service on the local computer.
    Error 1053 : The service did not respond to the start or control request in a timely fashion.
    What is the reason for this error ?
    Thanks.

    • @RoundTheCode
      @RoundTheCode  3 місяці тому

      You've either got the service name wrong, or there is a bug with your application. Make sure the service name you ran with the "sc" command matches what is in your app.

    • @shreyasmahajan2157
      @shreyasmahajan2157 3 місяці тому

      ​@@RoundTheCode
      Thanks for the reply!!
      Here is the the the way I am creating the service :
      sc create AIQuestionGeneration.WorkerService binpath="D:\Path\ToService\Build1\AIQuestionGeneration.WorkerService.exe" start="demand" displayname="AIQuestionGeneration.WorkerService"
      Any thing incorrect ? Or a Bug in my application ?

    • @shreyasmahajan2157
      @shreyasmahajan2157 3 місяці тому

      After banging my head for quite a few hours, I have sorted the issue.
      The issue was simple, the service was not able to find the app.settings.json file because I had used "Directory.GetCurrentDirectory()" in my Program.cs.
      As a result it was searching the file in the default location of system32.
      All what I had to do was change the path to : "Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)" and it worked in a second.
      The error which I got was not much informative. I had to visit the "Event Viewer" section in Windows to check the logs and figure out the exact error.

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

    Nice and easy, thank you. Btw, you look like a South Park character prototype 🙂

  • @mhsn27mhsn10
    @mhsn27mhsn10 7 днів тому

    thank you Sir

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

    Thank you!! Excellent stuff! It is possible to add a SignalR hub server to our Worker service??

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

      Hi bioan,
      A SignalR Hub requires a URL so clients can connect to it.
      As a .NET Worker Service is a background service rather than a web application, I would assume that you can't.
      However, you can run hosted services in ASP.NET Core and that would work. You could pass in your IHubContext into your hosted service as it has a Singleton service lifetime.
      Check out this video for more about ASP.NET Core hosted services.
      ua-cam.com/video/IekoUio2Fek/v-deo.html

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

      @@RoundTheCode Thank you very much!!!

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

    great explaination

  • @coding-gemini
    @coding-gemini 8 місяців тому

    Nicely explained Thank you, what are the other options for making it an automatic windows service, auto ?

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

    thank you short and clear

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

      Thanks Şükrü. Glad you found the content useful.

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

    What if you would need to call another Background service from the main Worker Service?

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

      Hi Gus,
      Would you want to trigger a background service to start from another Worker Service? Is that what you're asking?

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

      @@RoundTheCode Yes I have one Hosted Worker Service that runs initially and at some point of its execution I want to start another "child" background process. I'm currently triggering the child process with "await Task.Run(() => ChildBackgroundProcess, cancelToken)" but I'm not sure whether that is the correct\clean way to do it

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

      @@guswind4636 And I'm assuming you didn't add the child background process to the IoC/DI container as part of dependency injection? You just created a new instance of the child background process and ran it?

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

      @@RoundTheCode yes I didn't add the child process to the DI container, it's just a child Process\Task that runs indefinitely as long as the main Worker Service runs

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

      @@guswind4636 I personally don't see anything wrong with that, as long as the two tasks have some sort of relation, and the child task has to run on the parent task.

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

    I like your video. But I have a question whom will send the cancellation token to workerservice? and whats thw role of the loop while? is it to keep the WorkerService running? Thank you in adavnce

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

      Yes, the loop is to keep the Worker Service running. I believe the application will automatically send the cancellation token to the Worker Service when it stops.

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

    .Net Framework services are able to receive custom commands via "protected override void OnCustomCommand(int Command)". Is equivalent functionality available in .Net 6? I've looked at many examples of creating .Net 6 Worker Services but they all seem to use timers to monitor or report something periodically, none of the examples show receiving custom commands from other applications.

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

      Have a look at this. It might be what you're after? github.com/dotnet/runtime/issues/50021#issuecomment-547503684

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

    Thank you for the great tutorial! Is there any way to keep our windows service running even in sleep or hibernate mode? I just created a windows service and it's suspended (not stopped!) when I close the lid of the laptop.

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

      I'm not aware of anyway of doing it. If your computer is asleep, surely you wouldn't want it to be running any services?

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

    I've developed a routine that reads/updates data from specific database and then do a looping in the rows and returns status information to another application (webhook). It repeats every 24 hours. It works in this BackgroundService approach, but is it a good pratice? There is no much data read in this process, I guess about 150 per day. Thanks

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

      Hi Leivas. It sounds good to me. Is it 24 hours after the app has started? Or is it at a specific time of day?

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

      @@RoundTheCode it's at a specific time of day

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

    Hi can we add multiple job into the worker service using (Quartz)

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

      I've never used Quartz before, but I'm assuming you can do it.

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

    Thanks.

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

    I have a worker service and added the Setup Installer project but I haven't found a way to install the service using the installer. It works with sc.exe but how can I add this step to the installer?

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

      You could have a look at the WiX Toolset. I have created installers for .NET Framework services in the past. Guessing you could use it for later .NET versions?
      wixtoolset.org/

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

    I am getting error 193 when i try to start the service. When i try to start it from the console it says that it is not a valid win32 aplication. Does anyone know what could be causing it?

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

      I would check that you have the correct filepath to the .exe file when you registered the Windows Service. If the filepath doesn't match, you are likely to see an error like that.

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

    Good video... Is possible to make this service in an unstoppable service? Thanks

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

      Hi Toni,
      Thanks for your comment. Just out of interest, why would you want an unstoppable service?

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

      @@RoundTheCode Because the user should not stop it. It is a service that takes statistics from the computer.

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

      @@toniobrados6736 Check out this StackOverflow post. A user has come up with a possible solution. stackoverflow.com/questions/70503859/is-there-a-way-to-disable-the-stop-button-in-windows-worker-services-background

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

    Hi @roundtheCode, could we have work services in a different language like python on azure? I have been struggling with this for a while

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

      Hi Richard, unfortunately I don't know enough but Python to be able to answer your question.

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

    In file explorer, if you hold Shift + Right Click, the context window gives you an option, "copy as path" this copies the full file path inside double quotes, so you can paste it anywhere. Annoyingly, in Windows 11, you probably have to click, show more options on the dumb Windows 11 context menu.

  • @imterpsfan2
    @imterpsfan2 9 місяців тому

    Nice example. I have an existing Windows Service application in .NET Framework that I'm migrating to a Worker Service. Because I am using some Windows-specific aspects in the service, such as reading Windows Event Logs (EventLogQuery) I'm assuming I will need to encapsulate this functionality in the .NET Standard and then reference that project in the worker service?
    Wait, I see System.Diagnostics.EventLog ia now in nuget package manager as a .NET Platform service so maybe I don't need to do this trick anymore? The answer is that I don't need to encapsulate the project in .NET Standard and then reference in .NET Core. I can do it directly! But I need the Windows Compatibility Pack.
    dotnet add package Microsoft.Windows.Compatibility --version 8.0.0
    dotnet add package System.Diagnostics.EventLog --version 8.0.0

    • @RoundTheCode
      @RoundTheCode  9 місяців тому

      Looks like you've found out how to do it.

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

    thanks, nice video