✅ Dependency Injection Exception
I'm having issues accessing dbcontext in my dependency injection project. I get an exception
Everything works fine in the application layer, problem occurs when I try to access dbcontext in identity service
28 Replies
I have no idea what could be causing the error, is it not being resolved or something else?
it's right there at the very end
Cannot consume scoped service 'GymRadar.Application.Contracts.Persistence.IGymRadarDbContext' from singleton 'GymRadar.Application.Contracts.Identity.IIdentityService'.)db context is scoped, identity is singleton you cannot use scoped services from within a singleton
I use identityservice to take a token decode it into and id and attach the user to IdentityContext
I tried making it scoped but it also didn't work :I
let me see if it produces a new error
also why isnt db context transient? Isn't it supposed to destruct if dbcontext.save is not called?
oh yes I get a new error
because usually the scope will be something like a request in asp.net, or a command in a discord bot
you don't want be creating a bunch of them for no reason
also makes things like transactions work cross-service
so a single request holds only 1 scoped object?
and resets it afterwards?
no I mean
in asp.net, when a request comes in, it makes a new scope from the DI container
and instantiates your controllers and services from that scope
lets say I want to hold current user identity as a DI object
should that be transient like I made it be?
or is there a better method to attach user context when passing it to mediatr
no you'd probably want it to be scoped
what kind of app is this?
CRUD
asp.net
postgres
yeah I'd make it scoped
hydrate it from the http context or whatever
ty
also, why is your DbContext interfaced?
I hold configurations migrations and db context in another project
like this
using this as reference point
GitHub
GitHub - jasontaylordev/CleanArchitecture: Clean Architecture Solut...
Clean Architecture Solution Template for .NET 7. Contribute to jasontaylordev/CleanArchitecture development by creating an account on GitHub.
oh, Clean Arch
forcing myself not to put everything in single project
to avoid 50 folders
Still can't get it working, having issues with dependency injection
can't figure what I'm doing wrong
Hastebin
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
this is the latest code I tried
I call AddInfrastructureServices but it seems like it doesn't attach
I guess ident middleware is taken from root scope - you cant inject mediator there
mediator injection works, problem is the identity service
is there some example on how to handle authentication when working with mediator
I bet there is via search
i removed everything and still have the same issue
are there any special nugets needed for DI?
No
it resolved when I changed scoped to transient....
that's the only difference I spotted in my and example code
Giving commentary doesn't make it easy for anyone to help
idk what's wrong
i will ask when i figure it out