Background Tasks Are Finally Fixed in .NET 8

Sdílet
Vložit
  • čas přidán 20. 08. 2023
  • Use code ARCH15 and get 15% off the brand new Solution Architecture course on Dometrain: dometrain.com/course/getting-...
    Become a Patreon and get source code access: / nickchapsas
    Hello, everybody, I'm Nick, and in this video, I will show you how Background Tasks, Background Services or Hosted Services have changed in .NET 8. The changed in .NET 8 ultimately fix a lot of the complaints we had in previous versions.
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasGitHub
    Follow me on Twitter: bit.ly/ChapsasTwitter
    Connect on LinkedIn: bit.ly/ChapsasLinkedIn
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

Komentáře • 148

  • @serus164
    @serus164 Před 9 měsíci +134

    Classic 'hellœverybody' always brightens my day

    • @yearlyoatmeal
      @yearlyoatmeal Před 9 měsíci +13

      "euhlihstyocontent" if you add up all the time nick saves on not pronuncing consonants in his intros it'll be thousands of years. Time he generously puts towards giving us the best dotnet content in the world.

    • @EricPhillips89
      @EricPhillips89 Před 9 měsíci

      ​@@yearlyoatmealI actually don't know what that part is 😂

    • @yearlyoatmeal
      @yearlyoatmeal Před 9 měsíci +2

      @@EricPhillips89 it's "if you like this type of content" :)

    • @yearlyoatmeal
      @yearlyoatmeal Před 9 měsíci +1

      he explained in a comment a while back that the reason he speaks so fast has to do with viewer retention. In my humble opinion he would win by speaking a bit slower and clearer and then speeding the whole thing up afterwards. But I'm grateful for the great content anyways

    • @rodrigodearcayne
      @rodrigodearcayne Před 5 měsíci

      Also: “I’m Nick and in this video…” becomes “I’m naked in this video” 😂

  • @veselinvalkanov
    @veselinvalkanov Před 9 měsíci +6

    OOOHHhh man if this new feature was released a few weeks earlier it would have saved me a lot of time. I had the exact same problem with worker-services management and had to come up with a custom solution. After this video I will update my project to .Net 8 :)
    This video seems to be made especially for me, thanks for everything. You are the best!

  • @Nekroido
    @Nekroido Před 9 měsíci +3

    I implemented my own version of IHostedLifecycleService with exactly the same functionality for my pet project. I had a bunch of background processes sitting side by side and posting stuff to the shared in-memory message bus. Discord bot, Twitch chat bot, Twitch API polling, database storage abstraction, and REST API to manage all that. All have to start concurrently, and all have to be aware of the whole lifecycle of the application (starting, started, stopping, stopped), so they don't message into the void and clean stuff up properly. And I build the first PoC for .NET Core 2 which is now as old as a first-grader, haha

  • @vmachacek
    @vmachacek Před 8 měsíci +2

    i did use background service for many things and usually with the new timer which i found out about on this channel, thanks

  • @CRBarchager
    @CRBarchager Před 9 měsíci

    Very nice explaination. I'm actually developping a service right now that can used this feature. I'll be looking forward to implementing them in the future.

  • @mortentoudahl
    @mortentoudahl Před 9 měsíci +31

    Adding 'await Task.Yield' as the first statement will also prevent the background service from blocking

    • @f135ta
      @f135ta Před 8 měsíci +2

      I came here to say exactly the same thing! I've been doing that since day one with them!

    • @FrancoisNel256
      @FrancoisNel256 Před 6 měsíci

      ​​@@f135tathat wont really fix it. The host still awaits the StartAsync task. With Task.Yield, you just giving some other task in the queue to have a chance to do its work. Which is silly because at the time your StartAsync is called (assuming it is the first one), there is should be no other task in the TaskScheduler. And (without this new option) another StartAsync wont be called if the one before it has not yout completed.

    • @natan_amorim_moraes
      @natan_amorim_moraes Před 2 měsíci

      Outside the while loop?

    • @omfgiwantausername21
      @omfgiwantausername21 Před 2 měsíci

      ​@@natan_amorim_moraesyes

  • @Kingside88
    @Kingside88 Před 9 měsíci +22

    Thank you Nick. Great explanation! I think BeforeStart, Start and AfterStart would be less confusing.

    • @nitrovent
      @nitrovent Před 9 měsíci

      I guess that naming is a Microsoft thing.

    • @Kingside88
      @Kingside88 Před 9 měsíci

      @@nitrovent sure. I have no complaint against Nick 😀

    • @nitrovent
      @nitrovent Před 9 měsíci

      @@Kingside88Well I didn't think so ;) Just wanted to emphasize Microsoft being Microsoft. I know >10 years old SharePoint code uses that naming convention e.g. in their feature receivers...

  • @apolo1805
    @apolo1805 Před 9 měsíci

    Thank you Nick, this was so insightful

  • @deeplerg7913
    @deeplerg7913 Před 9 měsíci +26

    I think we should also have a per-service boolean that indicates whether the service can start in parallel or not. You might have multiple background/hosted services, and some of them might need to start before the application starts, while others might not care as much

    • @georgepagotelis
      @georgepagotelis Před 9 měsíci

      When to use channels in C#?
      Channels in C# are designed based on the single-producer, single-consumer (SPSC) pattern. They provide a reliable and efficient way to pass messages between producers and consumers in a concurrent system. The design of Channels in C# is optimized for scenarios where a single consumer processes the messages.18 May 2023
      If you know, you know.

    • @davilinkicefire
      @davilinkicefire Před 9 měsíci +2

      I would prefer having 2 separate class named "BlockingBackgroundService" and "NoBlockingBackgroundService"

    • @prom3theu581
      @prom3theu581 Před 9 měsíci

      I agree - this breaks anywhere where you are doing things such as migrations on app startup. The fact that IHostedServices ran in the order that they are registered makes them perfect for ensuring something is executing before the app framework launches. For example, MartenDb changed Db initialisation into an IHostedService in their latest version. Many libs handle it this way. I'd say there needs to be a separate interface which filters certain registrations to run in the previous format, and have the rest covered by IHostedService. Perhaps IStartupService? Technically these absolute run fifo services that we still need dont usually persist as background services for the lifespan of the app, once the task is finished, thats it - they are done. I always felt IHostedService was a bit misleading for those. This is purely unless all 'starting' tasks on the lifecycle service are guaranteed to run before any 'start' tasks, even if executed concurrently. If that is the case, then the new interface is exactly what we need

    • @parlor3115
      @parlor3115 Před 9 měsíci +1

      @@georgepagotelis Channels aren't necessarily single producer nor consumer. You can have multiple of either. The only restriction they have is that a message may only consumed once. Also, they're not as good as Golang's channels since they don't allow capacities of 0 (aka producer-consumer synchronization).

    • @georgepagotelis
      @georgepagotelis Před 9 měsíci

      @@parlor3115channel.Reader.TryPeek() doesn’t consume the head message

  • @jelle2819
    @jelle2819 Před 9 měsíci +2

    If you look at the implementation of the BackgroundService class you will see how starting with Yield or Delay will make it non-blocking for other services starting. I am always amazed that most programmers don't get that the first part of the async method executed in the time slice of the caller. Until it yields. Using Task to start off async work is thus tricky and that is where the blocking issue originates.

    • @slowjocrow6451
      @slowjocrow6451 Před 9 měsíci

      Do you know any clearly explained tutorials about this? I'm really struggling to understand yield etc

  • @rogeriobarretto
    @rogeriobarretto Před 9 měsíci +2

    Excelent stuff coming up in .NET 8, thanks for the update Nick! Great video!

  • @T___Brown
    @T___Brown Před 9 měsíci +1

    Never had this issue. But i usually startup immediately and cache stuff later vs heavy startup.

  • @garcipat
    @garcipat Před 9 měsíci

    We use background services for db migrstions and to keep observables for signal R or other reactive tasks.

  • @mazingguitar
    @mazingguitar Před 9 měsíci

    Yup, introducing new hooks is a big thing if there is need to do some async work on app closing for example. Great sharing, Nick!

    • @parlor3115
      @parlor3115 Před 9 měsíci +2

      Why can't you do them in StopAsync?

    • @user-dc9zo7ek5j
      @user-dc9zo7ek5j Před 9 měsíci

      @@parlor3115 I think you can also do it after app.RunAsync stops.

    • @mazingguitar
      @mazingguitar Před 9 měsíci

      antipattern really and it will not work for some long pending async jobs and you have to watch out for cancellation tokens so fighting the framework feels like not a very good idea

  • @JustinAdler
    @JustinAdler Před 9 měsíci +11

    Simple reason to use a 'blocking' hosted service: DB migrations. Your migration might take a bit of time. Don't want the web app to start accepting connections when the migration hasn't completed - could mean your code would error because the DB is not in the correct state.
    Background hosted service not blocking are also great for other things, instead of having to use hangfire or Azure Functions etc. (keep them light weight though, peeps. might need to consider scaling out, etc).

    • @johnnyblue4799
      @johnnyblue4799 Před 9 měsíci +10

      Migrations don't really belong on the start, but on the deployment pipeline... at least that's what I do.

    • @KieranFoot
      @KieranFoot Před 9 měsíci

      Or you could trigger your migrations from an IHost extension, giving you access to application services and blocking the app start as intended. I have never felt the need to block the start of a hosted service and if I did something that blocks, I would fire another Task anyway to do the work.

    • @mAcCoLo666
      @mAcCoLo666 Před 9 měsíci +3

      Then run your migrations as part of your application startup pipeline. A bg service should run in the bg...

    • @johnnyblue4799
      @johnnyblue4799 Před 9 měsíci +1

      @@mAcCoLo666 If your app has the right to run DDL scripts your security is compromised. The db user that your app uses should have r/, or r/w access at the most and execute if you're calling stored procedures.

  • @cleitoncarlospereira2006
    @cleitoncarlospereira2006 Před 20 dny +1

    Excellent content! I searched here but I couldn't find the explanatory video about the differences between Background Services and HostedService, could you give me the link please?

  • @namewastaken360
    @namewastaken360 Před 9 měsíci +1

    I think the only way to rationalise the names is that they used start and stop already and needed a name for an event before start and stop, and starting and stopping was the best they could come up with

  • @jamel056
    @jamel056 Před 9 měsíci

    thanks for your amazing videos. i have a question : could we use background service to launch it at the stating of the project and use it to store some configuration in for example: a json file for the first time we launch the whole project ? and what is the best scenario for this situation ? thank you

  • @williameduardo7020
    @williameduardo7020 Před 26 dny

    I used an azure function as my background service. But involves passing a message from one system to another.😊

  • @ewilliams0305
    @ewilliams0305 Před 9 měsíci

    I just wrote a service yesterday injected into a background service to assist with blocking my applications startup. Can’t wait to upgrade and delete everything 🎉

  • @KieranFoot
    @KieranFoot Před 9 měsíci +19

    This is one "fix" I wish they didn't make. There is a very good reason that BackgroundService is written the way it is and I do not agree that using the currently provided methods for background work are broken at all. Now a single host-wide setting might have unintended consequences when using 3rd party libraries.

    • @andreaspetersen2662
      @andreaspetersen2662 Před 9 měsíci

      Brainlessly using 3rd party libs without Reading docs, or using bad ones that have no docs always had some sort of consequence

    • @FrancoisNel256
      @FrancoisNel256 Před 6 měsíci +1

      My exact sentiment. We have written libraries with a very good reason to have one IHostedService.StartAsync not start before another has. Now, some ignorant dev is going to enable this new option for (maybe a legitimate reason for one usecase) and potentially causing a trainsmash with other IHostedService implementations. If anything, they should have made this an option per implementation.

  • @kudorgyozo
    @kudorgyozo Před 9 měsíci

    so... where do you put the work in the second example? The IHosted(LifeCycle)Service? Do you create a timer manually to periodically do stuff?

  • @CricketThomas
    @CricketThomas Před 9 měsíci

    I was just using these in my app and i was wondering "how did i miss this video?!" only to realize it was uploaded 14 minutes ago lol

  • @kjashzhvyktsul
    @kjashzhvyktsul Před 9 měsíci +6

    We use background/hosted services a lot. Typically we solve the blocking nature of them by starting worker task in startasync, storing task in private member and cancelling it in stopasync.

    • @fopsdev3676
      @fopsdev3676 Před 9 měsíci

      i'm just curious. using a bg service, how do you prevent iis from recycling/stopping the bg service?. i've tried miriads of configurations options but at not avail. we basically need the bg service to be up and running even the page doesn't get used.

    • @kjashzhvyktsul
      @kjashzhvyktsul Před 9 měsíci

      @@fopsdev3676 we don't really use iis, httpsys on windows and kestrel on linux. Hosted services are mostly used in backend webapi-s. We didn't have any issues like you described.

    • @KieranFoot
      @KieranFoot Před 9 měsíci

      Just as everyone else does... This is not a fix.

    • @kjashzhvyktsul
      @kjashzhvyktsul Před 9 měsíci +2

      @@KieranFoot I didn't say it's a fix. It's a solution that works. I think there is no need to fix anything. Sometimes you need to block the start of the service sometimes you don't and there is always a way. And you can create small simple abstraction layer, extract it into library and re-use it across all your services. The only problem is that not everyone is aware of current behavior.

    • @user-dc9zo7ek5j
      @user-dc9zo7ek5j Před 9 měsíci

      @@fopsdev3676 Background services only get stopped if the application stops or you call the StopAsync from another part of your code. If you are using timers or tasks on StartAsync you have to store them in a field in order for them to not be disposed. Backgronud services have nothing to do with pages.

  • @michaelakin766
    @michaelakin766 Před 9 měsíci

    Good info. just FYI though, the audio sounded very weird today.

  • @TeaVR
    @TeaVR Před 9 měsíci +2

    Be useful to have a real world example showing which method you should actually do the background work in and to show how Starting, Start, Started etc should be used

  • @THE_NE0
    @THE_NE0 Před 9 měsíci

    Can you create and remove background jobs programmatically like hangfire ?

  • @serus164
    @serus164 Před 9 měsíci +6

    Still missing feature is what to do if I wanted some of the services to start concurrently and some in strict order?

    • @W1ese1
      @W1ese1 Před 9 měsíci +3

      Yep, the same thing came to my mind. I want to be able to configure on a service level whether I care if it's synchronously executed or not

  • @tidianediallo6199
    @tidianediallo6199 Před 5 měsíci

    good job 🙂🙂🙂🙂

  • @arielunanue4354
    @arielunanue4354 Před 9 měsíci +2

    It's a great video, but it is a bad practice to put that kind of code (calls that might take a while to complete) in the startAsync. Just move all that code to the ExecuteAsync method.

  • @user-xm4oo5dp7x
    @user-xm4oo5dp7x Před 2 měsíci

    I still cannot understand why dotnet team didn't introduce something OOB for running anonymous background tasks via background job and default queue channel for it. Like some service where I can put my Task, get tracking Id for it and just to be sure that it will be executed eventually (something similar to what Hangfire do, event it's just in memory queue). It is strange that I need to implement custom service for which individual peace of domain logic, taking into account that it could be done in domain-agnostic way easily

  • @bjmmedeiros13
    @bjmmedeiros13 Před 9 měsíci +1

    Let me see if I understand correctly: the new interface IHostedLifecycleService should be used when you need to execute something once at application start (or stop), and the BackgroundService should still be used for tasks like periodically polling a queue?

    • @grumpydeveloper69
      @grumpydeveloper69 Před 9 měsíci +2

      Thats how I picked it up as well. I know I have seens IHostedServices that just spin up a long running background Task in the StartAsync to get something like the BackgroundService. I have lso thought that that (how we picked it up) is not actually how it is ment to be used.

    • @Mark-px3rq
      @Mark-px3rq Před 9 měsíci

      That’s how BackgroundService implements it.

  • @phnxbr
    @phnxbr Před 3 měsíci

    Thank you. I was developing my kludge workaround when I found your tutorial. I also would like to fail the whole service if some of the background services didn't went live/recovered after some time. For example, during lost connection.
    Actually, your example didn't help me. The whole service froze

  • @JohanNordberg
    @JohanNordberg Před 9 měsíci +4

    What would be the best way to execute a background service on a schedule now? I think I remember you have a video about this in the past, but what is best now with .net 8 and these improvements? Say, like, I want to execute a job every hour, or at 01:00 AM every day.

    • @chris-pee
      @chris-pee Před 9 měsíci

      Doesn't seem like there are any improvements on that front, so your best option is still manually using PeriodicTimer in a BackgroundService.
      Google "Run and manage periodic background tasks in ASP NET Core 6" for a pretty good article on that.

    • @justgame5508
      @justgame5508 Před 9 měsíci

      Way I do it is similar to what’s discussed here in the ExecuteAsync method await some Task.Delay(TimeSpan). To get the timespab you need there is a great library called Quartz that effectively parses Cron schedules. You can get the time delta from now -> next cron schedule execution and await that in the Task.Delay

    • @igelineau
      @igelineau Před 9 měsíci +1

      Hangfire comes to mind

  • @OsamaALSHABANi
    @OsamaALSHABANi Před 5 měsíci

    I have question here about this background service ,once we deploy the web app to IIS the background service would not work until a request coming from any client! ,otherwise it will not started
    how could i fix this ?

  • @colincbcampbell
    @colincbcampbell Před 9 měsíci

    Hi..
    Are these background services and hosted services still seen as singleton?
    Reason for the question of course revolves around running EF Core databases or scoped dependency injections that would still require using / creating the service scope, unless you bring the DBContext in as singleton, which is highly frowned upon..

    • @hyperpaus
      @hyperpaus Před 9 měsíci +1

      You can inject a DbContextFactory as Singleton, then create the DbContext when you need it. (And dispose of it quickly). This is better than having to inject IServiceProvider . For Service that don't have such a factory alternative, I'm afraid injecting IServiceProvider is the way to go.

    • @colincbcampbell
      @colincbcampbell Před 9 měsíci

      @@hyperpaus- Thanks.. that's exactly what I am doing at the moment - creating a db scope from the DBContextFactory when i need to get information out of the DB..
      I am, however, exploring one level up now - aka creating scope in the singleton wrapper class for a scoped class and dependency injecting the DBContext into the scoped class..
      I will feedback once working.. 😉

  • @jasoncox7244
    @jasoncox7244 Před 9 měsíci

    This is /exactly/ what I need right now lol. I have a particular service that depends on a rediculous Google Calendar API query (like upwards of 40seconds retrieval time); but once that data is retrieved, it isn't likely to change very often. So I have a service that Caches that query result and periodically checks for changes while the end users get fast results from the cached data. This will make that service so much cleaner.

  • @saravananani4933
    @saravananani4933 Před 4 měsíci

    Hi Nick, Unable to call the external WCF by adding the URL as service reference in the C# lambda function . can you please help me on this . when I am adding as a service reference its not recognizing the service client and method contracts .

  • @timh8324
    @timh8324 Před 6 dny

    Im trying to fix my issue where if it takes a little too long to shutdown and you had used the restart service to restart it - it will fail restarting because it exceeds the timeout.

  • @ashishoffline
    @ashishoffline Před 2 měsíci

    Does this change anything (except for IHostedLifeCycleService interface) for Worker Service project with multiple Background Service ? Basically does ServiceStartConcurrently has any impact ? It isn't available in WorkerService project.

  • @br3nto
    @br3nto Před 9 měsíci

    I wish I had the ability to stop and start and add and remove hosted background services myself at runtime.

  • @aracon9721
    @aracon9721 Před 9 měsíci

    Is Solution Architecture something like CQRS which you only need when you develop on really large applications? So I probably wont ever need it?

  • @RasulAbuMuhammadAmin
    @RasulAbuMuhammadAmin Před 7 měsíci

    The annoying problem in these services is scaling. When you run multiple pods with your app, and each of them running their own background services, when what you really need is only one. (Like db interaction, external api call, etc).

  • @Thenewvoice-pc5re
    @Thenewvoice-pc5re Před 13 dny

    You missed one important topic. Communication between the background/hosted service and the main thread but brillant otherwise.

  • @sergeybenzenko6629
    @sergeybenzenko6629 Před 9 měsíci

    In the background .NET just puts all hosted services into group of tasks and does await Task.WhenAll(). So the difference with the prevoius approach is that several hosted services start concurrently between themselves as opposed to being started one after another in order of registration, not that the application now starts concurrently with the background services. So, I'm not sure that your statement at 7:29 is accurate.

  • @pavfrang
    @pavfrang Před 9 měsíci

    What is the purpose of having a Starting and Stopping handlers? This was not clear. Aren't the Start/Stop handlers sufficient?

  • @soft.developer
    @soft.developer Před 4 měsíci

    Timezone is always wrong. What can i do to fix it?
    4 or 5 hours of differences

  • @KlemensSoftware
    @KlemensSoftware Před 8 měsíci

    Imagine you have one background service that creates a request every minute to some third party API. Now also imagine if you have multiple instances of your webapp running. Does this mean that every instance will make a request every minute? How would you limit execution of a background service to one instance?

  • @UweKeim
    @UweKeim Před 9 měsíci +12

    I've used Background Services in the past, but still prefer a good old C# console application that is cyclically ran by the Windows Task Scheduler. In my 20+ years of experiences this beats every other technology, including Windows Services.

    • @Benke01
      @Benke01 Před 9 měsíci +7

      Will work great in Linux Docker. 🙃

    • @UweKeim
      @UweKeim Před 9 měsíci +2

      @@Benke01 Honestly, I do not know, as I'm nearly using Docker close to never. I would assume that it also could run just as well as a cronjob?

    • @johnnyblue4799
      @johnnyblue4799 Před 9 měsíci +6

      Windows Services have a different use case. It's not a substitute for scheduling tasks, although you can use them that way.

    • @Benke01
      @Benke01 Před 9 měsíci +2

      @@UweKeim As it is now it will be platform independent and you can easily use it in the cloud or just spin it up locally without having to do any Windows service setup. A cronjob however run on a timed schedule. You can mimic that with a background service but less conveniently.

    • @Nekroido
      @Nekroido Před 9 měsíci +1

      This also naturally fits into distributed/microservice architecture. I can have multiple chat bots, DB abstractions, and a REST API in one executable to run on my gaming PC, but putting them into separate Docker containers gives me way more control and flexibility.

  • @TampaCEO
    @TampaCEO Před 9 měsíci +12

    For many years, JAVA ruled the business software world due to its ability to run on multiple platforms including Linux (which allows it to run in containers).
    Ever since .NET core came out, however, that advantage was erased. When you combine this fact with the .NET framework & the incredible advancements to the C# language, I can't see why anyone would choose JAVA over .NET today. Thanks for another great video.

  • @HokKeinInder
    @HokKeinInder Před 6 měsíci

    Will this be functional in Maui?

  • @metaltyphoon
    @metaltyphoon Před 9 měsíci +4

    Ok now tell me: why couldn’t they add the new interface methods on the IHostedServices interface and have a noop for those extra methods?

    • @SytheZN
      @SytheZN Před 9 měsíci +6

      Adding them to the interface would force everyone who has implemented that interface to update their code to include the new members, and would break if you tried to run anything that was compiled against the older interface on the newer one.

    • @qj0n
      @qj0n Před 9 měsíci +1

      ⁠@@SytheZN...unless they added default implementations for them.
      However I think it's still would be an overkill to have 6 methods on simple IHostedService interface, now it's easy Start and Stop, more complex cases are quire rare anyway

  • @marcome1953
    @marcome1953 Před 9 měsíci

    Is Hangfire still a good choice to manage background tasks in 2023?

    • @FrancoisNel256
      @FrancoisNel256 Před 6 měsíci +1

      Yes. It has its place. But Hangfire an IHostedService is not in the same category. IHostedService is tied to the lifetime of a hosted service. Hangfire runs tasks on a schedule.

  • @kaiserbergin
    @kaiserbergin Před 9 měsíci

    No more yielding that long startup task!

  • @brightshadow9480
    @brightshadow9480 Před 9 měsíci +2

    You should've passed your cancellation token to your Task.Delay()

  • @blaze9872
    @blaze9872 Před 2 měsíci

    It's features like these that make me want to migrate from framework

  • @lizard450
    @lizard450 Před 6 měsíci

    ... doubt it...

  • @ahjsbkdjhavkcjhvac
    @ahjsbkdjhavkcjhvac Před 9 měsíci

    update ur ride man :D

  • @maththaioseleutheriaphilos2320

    And how is dbcontext running now? Does it still need Scope Factory?

  • @bluecup25
    @bluecup25 Před 9 měsíci +1

    Hellovrybody

  • @Velociapcior
    @Velociapcior Před 9 měsíci

    I use Coravel for Queues

  • @georgepagotelis
    @georgepagotelis Před 9 měsíci +3

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
    *await Task.Yield();*
    ^-- blocking nature does NOT exist if you you yield control back to the minimal api (app whatever) you're running while your backgroundhost service takes "20 seconds". No need for that fancy code.
    Microsoft need to fix two things, when published it "automatically" kicks off instead of having to hit the URL endpoint and application lifetime starting/stopping (i'm hoping has been fixed cause it's a little weird knowing if deployment or it's crashed etc).

  • @gavinlangley8411
    @gavinlangley8411 Před 9 měsíci +2

    Don't do it at all. Post a message to a queue and expose and API instead.

    • @KonKri
      @KonKri Před 9 měsíci +2

      Sometimes a queue is too much of a pain for simple tasks.

  • @DigitalNomadOnFIRE
    @DigitalNomadOnFIRE Před 9 měsíci

    Nick The Greek....

  • @JackBauerDev
    @JackBauerDev Před 9 měsíci

    Microsoft? You mean .Net foundation ;P

  • @timur2887
    @timur2887 Před 9 měsíci

    Such a long jorney for MS to make a clean code... And this is not a rocket science after all

  • @jamesdraughn5072
    @jamesdraughn5072 Před 9 měsíci +1

    Just an FYI, every time I see the words finally, insane, genius ect.. I go out of my way to NOT watch the video, even if it's from a content provider I am subscribed to. If it happens enough, I end up unsubscribing.

  • @MrFoxPro
    @MrFoxPro Před 9 měsíci

    Thank god I'm not using c# anymore

  • @artemivanov2141
    @artemivanov2141 Před 6 měsíci

    What is the name of video about Hosted Service on this chanel? I can't find it @nickchapsas

  • @marcobaccaro
    @marcobaccaro Před 2 měsíci

    Welcome back to the Global.asax

  • @mortentoudahl
    @mortentoudahl Před 9 měsíci +35

    Adding 'await Task.Yield' as the first statement will also prevent the background service from blocking

    • @Mark-px3rq
      @Mark-px3rq Před 9 měsíci

      How does it manage that?

    • @thomai5287
      @thomai5287 Před 9 měsíci +6

      @@Mark-px3rq Yield will return from the Task and continue in the background.

    • @mAcCoLo666
      @mAcCoLo666 Před 9 měsíci

      But why waiting synchronously on a bg service? Especially seen as you can make it await.

    • @justengineering1008
      @justengineering1008 Před 9 měsíci

      configureAwait does not fix this, does it? it is obvious that “await” by default is running in the same thread.

    • @mortentoudahl
      @mortentoudahl Před 9 měsíci

      @@justengineering1008 No it doesn't. That's why I didn't mention configureawait ;-)