dotnet / dotnet/aspnetcore

AggregateException happens if exception occurs before AddRazorRuntimeCompilation()

Open
#29,703 13 comments 0 reactions 0 assignees View on GitHub
area-mvc bug feature-runtime-compilation
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

A change appears to have been made between .NET Core 3.1 and 5.0 which causes an `AggregateException` to happen if, in an ASP.NET Core web app, an exception occurs before `.AddRazorRuntimeCompilation()` occurs.

I wrap my `ConfigureServices` code in a try/catch block so that if an exception occurs, I can catch it and put out a friendly, basic, HTML error message. In .NET Core 3.1, this would break into my `catch` block, and my code would be able to output a basic message with the "Hello world" exception message:

```
throw new Exception("Hello world");
services
.AddControllersWithViews(opt => {
opt.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})
.AddRazorRuntimeCompilation();
```

In 5.0, my catch block never runs, and I get an immediate `AggregateException`:

```
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.Compilation.IViewCompilerProvider Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompilerProvider': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompilerProvider'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CSharpCompiler Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CSharpCompiler': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager'.)
```

This is a rather unfortunate change as it means the only safe way for me to use the Razor runtime compilation is to put the call right at the top of my `ConfigureServices`. I assume something weird is going on at build time, too, because if I remove the call to `.AddRazorRuntimeCompilation()`, the exception is once again caught in my `catch` block.

I think the behaviour should be reverted to how it was in .NET Core 3.1.

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.