Dependency injection failure when manually invoking IServiceCollection.BuildServiceProvider
See original GitHub issueIs 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:
- Created 3 years ago
- Comments:7 (1 by maintainers)
cc @OsmondJiang @junshi356 for awareness.
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.