👉 When your ExceptionHandlingMiddleware tries to resolve a scoped service directly from the app root container
(which is NOT allowed).
The fix is simple — and you’re very close.
Let me give you the correct, production-safe version.
✅ Root Cause
Your middleware is registered as a singleton (default), meaning:
-
Middleware = Singleton
-
ErrorLoggerService = Scoped
➡ Middleware cannot directly use scoped service from constructor
➡ Must resolve inside InvokeAsync() using context.RequestServices
You already attempted this — but the middleware itself still injects the service incorrectly.
✅ Correct Fix (100% works)
❌ WRONG (current middleware constructor injects scoped service)
This forces the DI system to resolve IErrorLoggerService at startup (root provider).
→ Boom: exception.
✅ ✔ RIGHT: Middleware must NOT receive scoped services in constructor
✔ FIXED Middleware
Notice:
✔ No scoped services in constructor
✔ Scoped service resolved inside request pipeline → valid
🛠Program.cs validation (your DI is correct)
This is perfect.
No need to change DI.
No comments:
Post a Comment