dotnet / dotnet/aspnetcore

DatabaseDeveloperPageExceptionFilter can be broken when DbContext is in a different assembly

Open
#32,976 8 comments 0 reactions 0 assignees View on GitHub
area-middleware feature-diagnostics
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Describe the bug
When using `AddDatabaseDeveloperPageExceptionFilter()` a Database error should result in the "DatabaseErrorPage" being rendered, however in a configuration where the DbContext is in a different assembly which is not currently loaded, the DatabaseDeveloperPageExceptionFilter throws it's own exception and blank page is rendered with a 500 status code.

```
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
Npgsql.PostgresException (0x80004005): .........

fail: Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseDeveloperPageExceptionFilter[7]
An exception occurred while calculating the database error page content. Skipping display of the database error page.
System.IO.FileNotFoundException: Could not load file or assembly ', Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
```

The exception occurs here when attempting to get the service from the assembly:
https://github.com/dotnet/aspnetcore/blob/e6998cb7bb7aba3b803fe8ed2622a090f3fa0fc1/src/Middleware/Diagnostics.EntityFrameworkCore/src/HttpContextDatabaseContextDetailsExtensions.cs#L47

### To Reproduce
You need a solution which has a separate project containing the DbContext which is shared by more than 1 other project.

When the DbContext is in a separate project, a migrations assembly needs to be specified, so for example, one of the projects will need to be designated as the one from which migrations are performed, so this will need this to be specified in the project's AddDbContext options:
```
services.AddDbContext(options =>
options.UseNpgsql(connectionString, npgsqlOptions => {
npgsqlOptions.EnableRetryOnFailure();
npgsqlOptions.MigrationsAssembly("();` in a try catch and handling the null scenario to avoid this error?

### Further technical details
- ASP.NET Core version
5.0.202
- Include the output of `dotnet --info`
.NET SDK (reflecting any global.json):
Version: 5.0.202
Commit: db7cc87d51

Runtime Environment:
OS Name: Mac OS X
OS Version: 10.15
OS Platform: Darwin
RID: osx.10.15-x64
Base Path: /usr/local/share/dotnet/sdk/5.0.202/

Host (useful for support):
Version: 5.0.5
Commit: 2f740adc14

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.