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.

Dependency injection failure when manually invoking IServiceCollection.BuildServiceProvider

See original GitHub issue

Is your question related to a specific version? If so, please specify:

I don’t think so, but we are using 3.0.14916.0, if that helps.

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#.

Question

We have encountered a weird exception while the function host trying to activate a service:

Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IEnvironment' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Configuration.ScriptHostOptionsSetup'. 

We have traced this back to where we manually called IServiceCollection.BuildServiceProvider, and this exception is gone after we changed to use the ServiceProvider passed to our anonymous function as parameter from AddSingleton<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) where TService : class.

This exception doesn’t reproduce when we run it locally in Visual Studio, but happens online after we deployed it to App Service.

I have looked into the source code mentioned in similar issue #6783, also found a usage of IServiceCollection.BuildServiceProvider.

Could you help us understand this situation, and probably come up with a way of preventing this from happening in future?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bianyifancommented, Dec 10, 2020

cc @OsmondJiang @junshi356 for awareness.

0reactions
bianyifancommented, Jul 6, 2021

Well, I can’t reproduce this bug with the latest Azure Functions host extensions, probably this has been fixed in some commit. No further help needed, but thanks for your help anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling 'BuildServiceProvider' from application code results ...
ASP.NET Core will build the ServiceProvider automatically at runtime, therefore don't call the BuildServiceProvider() manually.
Read more >
ASP.NET Core Dependency Injection - Steve Gordon
In this post, I explain what an IServiceProvider is and how it can be created and later used to resolve dependencies in ASP.NET...
Read more >
ASP0000: Do not call 'IServiceCollection. ...
Consider alternatives such as dependency injecting services as parameters to 'Configure'. How to fix violations. Remove the call to ...
Read more >
Resolve Services In ASP.NET Core Startup
When working with dependency injection, we will configure our services once at the start of an application's lifetime and then call upon the ......
Read more >
New dependency injection features in .NET 6
In this post I talk about some of the new features added to the DI libraries in .NET 6, as well a performance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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