Skip to content

DatabaseDeveloperPageExceptionFilter can be broken when DbContext is in a different assembly #32976

Open

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 '<name of migrations 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:

var migrationsAssembly = context.GetService<IMigrationsAssembly>();

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<AppDataContext>(options =>
                options.UseNpgsql(connectionString, npgsqlOptions => {
                    npgsqlOptions.EnableRetryOnFailure();
                    npgsqlOptions.MigrationsAssembly("<name of current assembly");
                }));

As there can only be a single copy of the Migrations directory which will live inside one of the projects, the MigrationsAssembly name that is specified will have to be the same when the DbContext is used across other projects.

This is where the issue arises, if a database error occurs in a project which has the MigrationsAssembly set to a different project's assembly which is not a dependancy, it is not accessible as it's not loaded as part of the project.

I quickly realised that the only reason the MigrationsAssembly was specified on the other project was due to copy and paste of the AddDbContext config, so I removed the MigrationsAssembly line as it was redundant in the other project, however this did not fully resolve the issue as although the DatabaseDeveloperPageExceptionFilter no longer throws an exception, a blank page is still produced with a 500 status code.

The DeveloperExceptionPageMiddleware is still kicking in and can be seen as the last entry in the log:

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

There could be another reason for this so I may have to do some more digging.

There would be no harm in wrapping var migrationsAssembly = context.GetService<IMigrationsAssembly>(); 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middleswarefeature-diagnosticsDiagnostic middleware and pages (except EF diagnostics)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions