question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 6 regression causes UseEndpoints to throw an ArgumentNullException in MVC-only project w/ conditional compilation

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In the most recent project I updated from .NET 5 to .NET 6, I observed a curious regression involving an exception at startup that would only reproduce in a Development environment; the web application could be deployed to Production without any issues.

The exception is a Linq NullArgumentException(nameof(source)) thrown when the initial call to UseEndpoints(action => ...) is made, before the action callback is executed:

System.ArgumentNullException: ‘Value cannot be null. (Parameter ‘source’)’

This behavior is new to .NET 6; the same project can be either run or deployed as a .NET 5 project (after downgrading all ASP.NET Core libraries to v5.x) just fine w/out any exceptions. The exception stack trace included below.

Expected Behavior

No exception thrown.

Steps To Reproduce

After some head-scratching, I believe I have figured this one out. It takes place when a project has been configured/initialized with only MVC (services.AddControllersWithViews()) and without Razor Pages (invoking services.AddRazorPages() causes the exception to be avoided). Merely registering the razor pages service helper is sufficient (i.e. there is no need to actually add any razor pages to the project).

As for why this didn’t happen in production: the exception only occurs if mvcBuilder.AddRazorRuntimeCompilation() was called prior to invoking UseEndpoints(), which I had behind an #if DEBUG preprocessor switch.

So the following should reproduce the exception:

public void ConfigureServices(IServiceCollection services)
{
    var mvcBuilder = services.AddControllersWithViews();
    mvcBuilder.AddRazorRuntimeCompilation();

    // Add other services as needed
}

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    // Exception here:
    app.UseEndpoints(endpoints => {
        endpoints.MapDefaultControllerRoute();
    });
}

Exceptions (if any)

Complete Stack Trace (expand to view)
>	System.Linq.dll!System.Linq.ThrowHelper.ThrowArgumentNullException(System.Linq.ExceptionArgument argument) Line 12	C#
 	System.Linq.dll!System.Linq.Enumerable.OfType<Microsoft.AspNetCore.Mvc.ApplicationModels.IPageRouteModelConvention>(System.Collections.IEnumerable source) Line 15	C#
 	Microsoft.AspNetCore.Mvc.RazorPages.dll!Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionDescriptorProvider.PageActionDescriptorProvider(System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.ApplicationModels.IPageRouteModelProvider> pageRouteModelProviders, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Mvc.MvcOptions> mvcOptionsAccessor, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions> pagesOptionsAccessor) Line 39	C#
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 50	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, System.__Canon>.VisitCallSiteMain(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 54	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 91	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, object>.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 27	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(Microsoft.Extensions.DependencyInjection.ServiceLookup.IEnumerableCallSite enumerableCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 168	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, System.__Canon>.VisitCallSiteMain(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 54	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, object>.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 35	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 50	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, System.__Canon>.VisitCallSiteMain(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 54	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 91	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, object>.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 27	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 50	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, System.__Canon>.VisitCallSiteMain(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 54	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) Line 91	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, object>.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext argument) Line 27	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope scope) Line 29	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(System.Type serviceType) Line 163	C#
 	System.Collections.Concurrent.dll!System.Collections.Concurrent.ConcurrentDictionary<System.Type, System.Func<Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope, object>>.GetOrAdd(System.Type key, System.Func<System.Type, System.Func<Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope, object>> valueFactory) Line 1161	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) Line 125	C#
 	Microsoft.Extensions.DependencyInjection.dll!Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(System.Type serviceType) Line 41	C#
 	Microsoft.Extensions.DependencyInjection.Abstractions.dll!Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(System.IServiceProvider provider, System.Type serviceType) Line 56	C#
 	Microsoft.Extensions.DependencyInjection.Abstractions.dll!Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionEndpointDataSourceFactory>(System.IServiceProvider provider) Line 73	C#
 	Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) Line 594	C#
 	Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapDefaultControllerRoute(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) Line 53	C#
 	neosmart.dll!neosmart.Startup.Configure.AnonymousMethod__6_0(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) Line 195	C#
 	Microsoft.AspNetCore.Routing.dll!Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(Microsoft.AspNetCore.Builder.IApplicationBuilder builder, System.Action<Microsoft.AspNetCore.Routing.IEndpointRouteBuilder> configure) Line 110	C#
 	neosmart.dll!neosmart.Startup.Configure(Microsoft.AspNetCore.Builder.IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IWebHostEnvironment env, neosmart.StartupInitializer startupInitializer) Line 190	C#
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(object instance, Microsoft.AspNetCore.Builder.IApplicationBuilder builder) Line 32	C#
 	Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.ConfigureBuilder.Build.AnonymousMethod__0(Microsoft.AspNetCore.Builder.IApplicationBuilder builder) Line 21	C#
 	Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup.AnonymousMethod__1(Microsoft.AspNetCore.Builder.IApplicationBuilder app) Line 333	C#
 	Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.Configure.__MiddlewareFilterBuilder|0(Microsoft.AspNetCore.Builder.IApplicationBuilder builder) Line 23	C#
 	Microsoft.AspNetCore.Server.IIS.dll!Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.Configure.AnonymousMethod__0(Microsoft.AspNetCore.Builder.IApplicationBuilder app) Line 35	C#
 	Microsoft.AspNetCore.dll!Microsoft.AspNetCore.HostFilteringStartupFilter.Configure.AnonymousMethod__0(Microsoft.AspNetCore.Builder.IApplicationBuilder app) Line 18	C#
 	Microsoft.WebTools.BrowserLink.Net.dll!Microsoft.WebTools.BrowserLink.Net.HostingStartup.Configure.AnonymousMethod__0(Microsoft.AspNetCore.Builder.IApplicationBuilder app)	Unknown
 	Microsoft.AspNetCore.Watch.BrowserRefresh.dll!Microsoft.AspNetCore.Watch.BrowserRefresh.HostingStartup.Configure.AnonymousMethod__0(Microsoft.AspNetCore.Builder.IApplicationBuilder app)	Unknown
 	Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(System.Threading.CancellationToken cancellationToken) Line 105	C#
 	Microsoft.Extensions.Hosting.dll!Microsoft.Extensions.Hosting.Internal.Host.StartAsync(System.Threading.CancellationToken cancellationToken) Line 66	C#
 	Microsoft.Extensions.Hosting.Abstractions.dll!Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(Microsoft.Extensions.Hosting.IHost host, System.Threading.CancellationToken token) Line 64	C#
 	Microsoft.Extensions.Hosting.Abstractions.dll!Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(Microsoft.Extensions.Hosting.IHost host) Line 50	C#
 	neosmart.dll!neosmart.Program.Main(string[] args) Line 109	C#
 	neosmart.dll!neosmart.Program.<Main>(string[] args)	Unknown

.NET Version

6.0.200

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
RickBlouchcommented, Mar 15, 2022

I just ran into this while upgrading to .Net 6.0 as well. The exact same steps to reproduce apply in my situation as described by @mqudsi mqudsi

I would add that I am only seeing this error with Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package version 6.0.3. package version 6.0.2 works as expected.

4reactions
HemendraSinghcommented, Apr 25, 2022

This is a major bug in the Nuget package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation v6.0.4. This particular package (From .NET v6 onwards) has been having issues from the start. Please make the package stable and i request the dev team to prioritize it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why aren't my conditional compilation symbols working ...
I have a project which is targeting .NET Core 3. This project builds and runs fine in both Debug and Release configurations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found