dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

Dependency Injection errors are not shown when running locally #18856

Closed lukos closed 4 years ago

lukos commented 4 years ago

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:

.NET Core SDK (reflecting any global.json):
 Version:   3.1.101
 Commit:    b377529961

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
  Version: 3.1.1
  Commit:  a1388f194c

.NET Core SDKs installed:
  1.0.0 [C:\Program Files\dotnet\sdk]
  1.0.4 [C:\Program Files\dotnet\sdk]
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.100 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.507 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  2.2.104 [C:\Program Files\dotnet\sdk]
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]
  3.1.101 [C:\Program Files\dotnet\sdk]
Tratcher commented 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.

lukos commented 4 years ago

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.