Skip to content

DatabaseDeveloperPageExceptionFilter produces bizzare exception if model fails to initialize #56539

@Xymanek

Description

@Xymanek

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

DatabaseDeveloperPageExceptionFilter attempts to get IModels from all registered DbContextOptions:

// Look for DbContext classes registered in the service provider
var registeredContexts = errorContext.HttpContext.RequestServices.GetServices<DbContextOptions>()
.Select(o => o.ContextType)
.Distinct(); // Workaround for https://github.com/dotnet/efcore/issues/22341
if (registeredContexts.Any())
{
var contextDetails = new List<DatabaseContextDetails>();
foreach (var registeredContext in registeredContexts)
{
var details = await errorContext.HttpContext.GetContextDetailsAsync(registeredContext, _logger);

In our case, some of the registered DbContexts are expected to be used under very specific circumstances and throw an exception otherwise. Since GetContextDetailsAsync is not guarded against exceptions in any way, the "final" exception is the DbContext initialization failure, not the original exception, hindering debugging.

Expected Behavior

Original exception is clearly reported

Steps To Reproduce

  1. DbContext with
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        throw new Exception("Bad DbContext");
    }
  1. Throw exception in any endpoint (doesn't matter if controller/razor page/minimal api)

The logs will report Bad DbContext; likewise the debugger will break there.

Original exception will be buried very deep in debug output.

Exceptions (if any)

See above

.NET Version

8.0.300

Anything else?

.NET SDK:
 Version:           8.0.300
 Commit:            326f6e68b2
 Workload version:  8.0.300-manifests.5273bb1c
 MSBuild version:   17.10.4+10fbfbf2e

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.300\

.NET workloads installed:
 [aspire]
   Installation Source: VS 17.10.35004.147
   Manifest Version:    8.0.0/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.0\WorkloadManifest.json
   Install Type:        FileBased


Host:
  Version:      8.0.5
  Architecture: x64
  Commit:       087e15321b

.NET SDKs installed:
  8.0.300 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

This appears to be very similar to #32976, just under different circumstances

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions