Closed lukos closed 4 years ago
DI validation is enabled if your app runs in "Development". https://github.com/dotnet/extensions/blob/4ac4b19372773dc7a226309497495dcc26d9e26b/src/Hosting/Hosting/src/Host.cs#L119-L120
When running locally, are you running in Development? And in production? That will be logged at startup. It's controlled by the launchsettings.json file.
Locally, I run as "Local" to differentiate from our dev environment. In the deployed app, it is "Development" (on a dev server, not in production).
Ah, I can see the problem now! So obvious once someone tells you. Thanks.
Describe the bug
I am seeing errors related to the dependency injection container but only when deployed. When I run locally, I do not see the errors and the system runs fine.
To Reproduce
For example, I have accessed a scoped DbContext type in middleware. I know this is not permitted but since it worked locally, I thought maybe it was OK. It runs fine locally but once deployed, it rightfully goes bang with
Cannot resolve scoped service 'Blah.DbContext' from root provider
The same thing happened before with DI services that (correctly) did not validate since they did not have implementations. It worked fine locally (although those services weren't actually consumed) but once deployed, it blew up.
I cannot find any documentation that explains that the behaviour is so different between local and deployed, except for something that implied the opposite behaviour should happen - validation happens more strictly in development.
Further technical details
dotnet --info output: