© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
18 replies
becquerel

Dependency injection, but critical resources are only available after building the service provider

I'm trying to introduce dependency injection to a monogame project. For those unaware, Monogame gives you a
Game
Game
class with some crucial components (
GraphicsDevice
GraphicsDevice
and
ContentManager
ContentManager
) for getting stuff drawn to the screen. These components are only initialized after you construct your
Game
Game
instance and call
.Run()
.Run()
on it, making it 'live'.

So far I've been creating my service provider manually inside the Game class and using it as a faux-entry point. However, I really want to move the DI outside and resolve
Game
Game
like anything else. This is mainly because it'll let me use proper
async/await
async/await
, which I currently can't do. I have a mix of UI services that rely on the Game components (GraphicsDevice etc.) and 'pure' backend services.

I can't have separate service providers for backend and frontend services, because the frontend relies on the backend. Is there a way to 'add' services to an IServiceProvider after it's been built?

Can give more detail as necessary.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Dependency injection
C#CC# / help
6mo ago
✅ Dependency injection
C#CC# / help
2y ago
❔ Dependency Injection
C#CC# / help
3y ago
Dependency Injection Question
C#CC# / help
12mo ago