Aspnetcore: IIS Express in .NET Core 2.2 doesn't response if add .UseKestrel(options => options.AddServerHeader = false)

Created on 6 Dec 2018  路  9Comments  路  Source: dotnet/aspnetcore

Describe the bug

IIS Express doesn't response in .NET Core 2.2 if use a options.AddServerHeader = false
If remove this command, all work.

To Reproduce

Steps to reproduce the behavior:

  1. Run dotnet new mvc
  2. In Program.cs add .UseKestrel(options => options.AddServerHeader = false)
  3. In launchSettings.json add "ancmHostingModel": "InProcess" to "IIS Express" profile (or do it via Visual Studio in project debug properties page)
  4. Run the program on IIS Express
  5. See infinite loading

Expected behavior

The page opened.

Additional context

.Net Core SDK version: 2.2.100
Windows 10 1809

area-servers servers-iis

Most helpful comment

ANCM In-process doesn't use Kestrel, and by calling UseKestrel, you are re-adding Kestrel as the server. When ANCM tries to run, it launches with Kestrel instead. This is an ordering issue.

We recommend calling ConfigureKestrel instead of UseKestrel now: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#how-to-use-kestrel-in-aspnet-core-apps.

When you did dotnet new mvc, did you keep CreateDefaultWebHostBuilder?

All 9 comments

ANCM In-process doesn't use Kestrel, and by calling UseKestrel, you are re-adding Kestrel as the server. When ANCM tries to run, it launches with Kestrel instead. This is an ordering issue.

We recommend calling ConfigureKestrel instead of UseKestrel now: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#how-to-use-kestrel-in-aspnet-core-apps.

When you did dotnet new mvc, did you keep CreateDefaultWebHostBuilder?

When you did dotnet new mvc, did you keep CreateDefaultWebHostBuilder?

Yes.

Thank yo谐 very much! If i just replace UseKestrel with ConfigureKestrel it works! But of course with Server header, but i understand why.

Slightly unexpected behaviour of UseKestrel... Maybe you could add recommendation about ConfigureKestrel in migration guide (https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-2.2&tabs=visual-studio)?

@pakrym and I think we should have guarded against the scenario where someone calls UseKestrel after CreateDefaultWebHostBuilder. If it doesn't work, we will add it to the migration guide.

So I did a bit more investigation on this and we do throw an exception in this scenario and it is logged to Debug output. This is the exception I see:

C# System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server. at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

However, because we throw an exception in an app func (See: https://github.com/aspnet/AspNetCore/blob/master/src/Servers/IIS/src/Microsoft.AspNetCore.Server.IIS/Core/IISServerSetupFilter.cs#L30), it doesn't actually cause the server to fail to start. This means it still listens with Kestrel, causing the server to hang.

We will investigate making the error message better or if we can make the server fail to start.

We're not doing anything here

Thank yo谐 very much! If i just replace UseKestrel with ConfigureKestrel it works! But of course with Server header, but i understand why.

Is there any way to hide the server header when using IWebHostBuilder and InProcess hosting model?

Heyy in my case (Asp.net Core 2.2) i have following code at the end

        var host = whb.UseKestrel()
            .UseKestrel(options =>
            {
                options.Listen(new IPEndPoint(IPAddress.Loopback, 443), listenOptions =>
                {
                    var httpsConnectionAdapterOptions = new HttpsConnectionAdapterOptions()
                    {
                        ClientCertificateMode = ClientCertificateMode.AllowCertificate,
                        SslProtocols = System.Security.Authentication.SslProtocols.Tls,
                        ServerCertificate = certificate
                    };
                    listenOptions.UseHttps(httpsConnectionAdapterOptions);
                });
            })
            .UseStartup<Startup>()
            .ConfigureKestrel((context, options) =>
            {
                // Set properties and call methods on options
            })
            .Build();
        host.Run();

if i change UseKestrel to ConfigureKestrel my code stream never come to inside of the arrow function. But if i use the UseKestrel also or together than this error thrown. Should i use outofprocess instead inprocess. What is the right direction in this situation?

@dewelloper Please try to move your .UseKestrel content to the .ConfigureKestrel call.

I've just tried and found that it works for both IISExpress and console hosting.

csharp WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureKestrel(_ => _.ConfigureEndpoints()) .UseSerilog();

However I'm using dedicated configuration option in the appsettings.json to check should options.Listen to be called or not. The idea is to skip Kestrel's optsions.Listen if IIS(Express) hosting is used. And vice versa, to call options.Listen if the app is configured for self-hosting:

csharp public static void ConfigureEndpoints(this KestrelServerOptions options) { var services = options.ApplicationServices; var config = services.GetService<IOptions<CustomAppSettings>>()?.Value; if (!config.RemoteUrl.IsEmpty()) { var httpsCertificateConfig = config.HttpsCertificate; options.ConfigureEndpoints(config.RemoteUrl.Split(';'), () => KestrelServerOptionsExtensionBase.GetCert(httpsCertificateConfig)); } }

Yes bro i have always used this ConfigureKestrel before downgrading to 2.1. Because i couldnt sure about handlers working, couse i couldnt see debugger on 2.2 ConfigureKestrel listen handlers. But i tested it doesnt shown but working and again i upgraded to 2.2 now ok. Thanks

Was this page helpful?
0 / 5 - 0 ratings