Slow first page load when porting asp.net core from .net 5 to .net 6

Viewed 41

I have ported my application from asp.net core 5.0 to asp.net core 6.0 (following the recommended steps but keeping the .net core 5 hosting model) and I'm having a problem in dev: the initial load time of pages in the new version is very slow.

I have more or less pitpointed the issue to the use of Razor Runtime compilation:

        IMvcBuilder mvcBuilder = services
            .AddControllersWithViews(
                configure =>
                {
                    configure.InputFormatters.Insert(0, new RawJsonBodyInputFormatter()); // this allow receiving a JSON object as a string
                    configure.Filters.Add<PeppermintExceptionsFilter>();    //new UnauthorizedExceptionFilter());
                })
            .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization(option =>
            {
                option.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
            })
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            })
            .AddFluentValidation();

        if (environment.IsDevelopment())
        {
            mvcBuilder.AddRazorRuntimeCompilation(); // commenting out this line resolves the issue
        }

When the line is present, initial load for all pages takes up to 30 seconds (typically, the first page takes 30 seconds and other pages are often quicker).

If I comment out the option, then everything works fine again. This is clearly not a problem in production but it's really problematic (and frustrating) for developpement.

The problem is not present in asp.net core 5.0

Adding the AddRazorRuntimeCompilation option to a new .net 6.0 using the new hosting model) application seems to induce the same issue but I'm not sure because the compilation of pages in a nearly empty application is very quick.

1 Answers

(Sorry, I should have answered that long ago)

The cause of my problem is a bug in asp.net core SDK that won't be fixed until "later": the runtime and compiler uses, by default, different hash algorythm to figure out if a page has changed or not.

This causes all pages to be recompolied the first time they are accessed in debug mode when AddRuntimeCompilation is added.

The fix is to explicitely add this option to the project's file:

<PropertyGroup>
  <ChecksumAlgorithm>SHA1</ChecksumAlgorithm>
  ...

This fixes the issue.

Related