Upgrading ASP.NET web site to .NET 9
When upgrading a web site to .NET 9, the site stopped working with error 500.30 and this error was logged in event viewer:
System.InvalidOperationException: 'Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.HttpsPolicy.HstsMiddleware'. There should only be one applicable constructor.'
I could, unfortunately not, find any mentioning about this error on any of the search engines. There are a lot of information about the 500.30 error, but all of them was older and long before .NET 9. There are also many pages about the “Multiple constructors accepting all given argument types have been found in type xxx “, but none of them was about the HstsMiddleware.
The error “Multiple constructors accepting all given argument types have been found in type xxx” is a planned breaking change that was introduced in .NET 9 RC1 (https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/9.0/middleware-constructors). So, upgrading the site to .NET 8 worked fine. Also, removing the line app.UseHsts()
worked, but HSTS sounds like something that is good to have.
After upgrading the site to .NET 9 and then stripping down the site more and more, until almost nothing was left, the error disappeared when removing the line:
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
And that line is for Autofac.
And the Autofac NuGet package was not updated.
Updating Autofac (and Autofac.Extensions.DependencyInjection) solves this issue.