aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ServiceProvider is null for getting required services

opened this issue · comments

Hi

I was wondering if you guys could help me understand why Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider) is null when ILoggerFactory is requested as a service from Microsoft.AspNetCore.Routing.RouteBase.EnsureLoggers(HttpContext context).

ArgumentNullException: Value cannot be null.
 Parameter name: provider
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<T>(IServiceProvider provider)
Microsoft.AspNetCore.Routing.RouteBase.EnsureLoggers(HttpContext context)
Microsoft.AspNetCore.Routing.RouteBase.RouteAsync(RouteContext context)
Microsoft.AspNetCore.Routing.RouteCollection+<RouteAsync>d__9.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware+<ExecuteWithFilter>d__7.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()

I have a very badly named example project with a ServiceProvider implementation over here which also implements ISupportRequiredService. It is injected into my core app over here. I am not clear on why this line of code is failing. It appears that RequestServices on the context is null.

I have also tried building the projects locally so I can debug this, but they keep failing with all sorts of errors. Can you guys point me in the right direction for getting this going?

Any help much appreciated.

@fir3pho3nixx if you're implementing a custom IServiceProvider then I recommend you run the test suite https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection.Specification.Tests/. It's likely that you're not properly supporting all of the required semantics.

From the looks of it, you're not implementing scopes in your container.

@davidfowl - Nice one, will give it a go. Thanks.

From the looks of it, you're not implementing scopes in your container.

Interesting observation, I am registering these implementations here.

Good stuff! Heaps of failing tests ...