question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve async support on the startup path

See original GitHub issue

There are a few places where we lack async support that result in people making blocking call. These are using during application startup.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:69
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
davidfowlcommented, Jun 15, 2021

Perform async init in async Task Main and pass any object-graph into to TStartup or ConfigureServices without resorting to static state.

That was fixed in .NET 5 with this overload of UseStartup which lets you create the instance. Now you can use closures and ctor arguments again.

If you need services passed into Startup then make another DI container because the container isn’t mutable and that isn’t going to change.

Also the new hosting model removes some of the Program.cs/Startup.cs boilerplate so passing state in closures is possible and in a top level async main method. It makes the whole thing easier.

Guaranteed eager initialization of certain singleton services (async or not) prior to ASP.NET Core handling any incoming request.

https://github.com/dotnet/runtime/issues/43149 go add your +1. It’s not happening for .NET 6 though. As for adding the bool the AddSingleton. That’s a problem because it needs to be agreed upon by the other DI authors but if you can get consensus we can move forward with that design or we’ll have to pick another one.

Support for configuring async factories on ServiceCollection.

I don’t know how we’d support any of this in reality. The problem with async factories is that you need async constructors to inject them into. It’s viral, which means GetService would need to either block or throw. It also means that async factories can have both sync and async dependencies but sync factories can’t depend on async things (or it’ll be a pit of failure). This means you can’t constructor inject an async service. In your example above, where does IMyPreauthenticatedHttpClient get resolved?

We’d also need GetServiceAsync (as you described above), but not the generic overloads, that could be an extension method you wrote. BuildServiceProviderAsync (as you also describe) and a new ServiceDescriptor that 3rd party container authors could implement.

Anyhow, thanks for the write up. Those are real concrete problems that are hard to solve.

PS: it would be interested to start a discussion on async DI and see what others think. I don’t know how you’d solve the constructor problem but maybe we will find some interesting solutions.

EDIT:

In general, you’d want to split your IO graph from your non IO graph because they have very different performance characteristics. You want to be doing all of the impure stuff on the outside, and not have IO scattered within the construction of your dependency graph.

4reactions
soennekercommented, Feb 7, 2022

I’m definitely looking forward to this. Essentially everything underlying the application startup has or is moving to async, and making the startup path that way would improve the fluidity. Things like initializing a Service Bus listener, or connecting to KeyVault can’t even be done synchronously.

Hoping this gets in .NET 7

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I handle async operations in Startup.Configure?
I haven't provided any mechanism to cancel the async operation. I'm not sure how you can ... NET Core 3.x offers better support...
Read more >
The performance characteristics of async methods in C# - ...
Optimization #3: avoid async machinery on a common path. If you have an extremely widely used async method and you want to reduce...
Read more >
Getting Started | Creating Asynchronous Methods
Learn how to create asynchronous service methods. ... To start from scratch, move on to Starting with Spring Initializr. To skip the basics,...
Read more >
How to embrace asynchronous communication for remote ...
GitLab's entire team uses GitLab to collaborate asynchronously on all of our work. GitLab is a collaboration tool designed to help people work...
Read more >
Concurrency and async / await - FastAPI
Details about the async def syntax for path operation functions and some ... and doesn't have support for using await , (this is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found