Efcore: Custom IAsynQueryProvider does not resolve from my container

Created on 19 Jul 2018  路  5Comments  路  Source: dotnet/efcore

Hi all,

I cannot resolve a DbContext from my container when i use a custom IAsyncQueryProvider using optionsBuilder.ReplaceService<IAsyncQueryProvider, CustomQueryProvider>();. But i also cannot resolve my ILogger from my container.

Even when i resolve my IServiceProvider in my constructor i cannot resolve from there my other dependencies. And yes they are registered.

I got this possibility from https://stackoverflow.com/questions/46867613/is-it-possible-to-intercept-a-read-action

Can someone give me a explanation how i could do that? If i miss something? What are the possibilities?

Thanks!

closed-question customer-reported

All 5 comments

@Condra963 EF Core uses its own internal service provider for things, and that is the service provider your IAsyncQueryProvider will receive. It will not be able to access the 'main' service provider of your application (assuming that is where you have registered your ILogger and so forth) and so you will have to register your ILogger and other dependencies you want the IAsyncQueryProvider to access with the EF Core internal service provider, either by writing and adding an IDbContextOptionsExtension or building the internal service provider yourself and calling builder.UseInternalServiceProvider.

You should be able to get to the DbContext by instead taking ICurrentDbContext as a constructor and storing it in a field, then using its Context property wherever you need to.

@tuespetre Thanks for the quick answer, i will investigate this today. If it works i will close this issue.

What i was wondering, is there a way to use your own service collection? Or is there a reason why they are separated?

@Condra963 I think what you are asking was already answered by @tuespetre: You can build your own DI container and pass it to a call to builder.UseInternalServiceProvider when configuring the context to use it.

@Condra963 See also my comment here: https://github.com/aspnet/EntityFrameworkCore/issues/12645#issuecomment-407547274 This describes how to use context.GetService as a bridge from the EF internal service provider to the application's service provider, and hence doesn't require that you take over building and managing the internal service provider.

@divega I tried to pass my own container to builder.UseInternalServiceProvider but that does not work for me. But i created a workaround so i can pass the necessary services to the EF Core container for now so i could continue my work.

Anyway, the replace AsyncQueryProvider works just fine for me once i made myself a way to add my necessary services. So thanks for the help.

Was this page helpful?
0 / 5 - 0 ratings