✅ DB context injection not working
hi, i have app like this -
and when i use MainContext in endpoint as argmuent it says - (screenshot).
example of endpoint
14 Replies
also, it works if put it into Using construction
where is it that you're abusing IoC lifetimes?
cause the error tells you exactly what's happening
you're trying to reuse an existing MainContext instance to perform multiple queries, in parallel
DbContext instances do not support parallel use (I.E. multi-threading)
each incoming HTTP request needs to use its own MainContext instance for performing queries
ASP.NET Core sets all this up for you by creating processing all HTTP requests within their own service scope
and by default, DbContext dependencies are registered as scoped
it's also possible, I suppose, that you're performing multiple queries within the same request, but not
await
ing one, so that the end up stepping on each other
however, it doesn't look like you're using async
/await
i mean, anyway i can't get
like
wait...
it works like this -
but don't works if literally same code is in controller
why would you expect it to work the same
¯\_(ツ)_/¯
if you look at documentation for building controllers, you'll notice it doesn't specify that parameter-injection is supported
not without an additional [FromServices] annotation on each parameter that you want to be populated from the IoC container
yeah is see
ef core was last topic
im on 6 yet
there is a bit different way
ef core or not, the normal method of dependency injection for controllers is via the constructor
if you want to use parameter injection, you have to use the annotation I mentioned
Dependency injection into controllers in ASP.NET Core
Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.NET Core.
ill look at that later 🙂 thx for help,
works this way