Issue with .ConfigureKestrel() method in .net Core
Asked Answered
C

2

6

I'm trying to create a very simple API with .net Core, and am exploring Kestrel. I am following the directions on this MS tutorial: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2

However, when I try to call the ConfigureKestrel method, Visual Studio tells me that "IWebHostBuilder does not contain a definition for 'ConfigureKestrel()' and no accessible extension method for ConfigureKestrel accepting a first argument of the type 'IWebHostBuilder' could be found (are you missing a using directive or an assembly reference?)"

I can't find any info on this, and I'm fairly sure I'm using the right libraries. Any help would be greatly appreciated - code is included:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApplication2
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();

        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureKestrel((context, options) =>
        {
            // Error with ConfigureKestrel method above
        });
    }
}
Contribution answered 16/2, 2019 at 11:28 Comment(3)
This is pretty sure an extension method from a namespace you do not import.Dogberry
It's probably Microsoft.AspNetCore.Hosting or Microsoft.AspNetCore.Server.KestrelIdeally
Even while using both of those namespaces it still doesn't seem to resolve the error.Contribution
I
4

I have created a sample application from scratch and this compiles:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureKestrel((a, b) => { });
    }
}

Can you check if this example works for you?

Please also check that you have the latest version of .Net Core, perhaps it was changed? The documentation is 2.2

Ideally answered 16/2, 2019 at 13:26 Comment(1)
My issue is that I was running 2.1 - thanks for the answer!Contribution
S
2

Found the VS2017 ASP.NET Core Web Application -> API running 2.1 does not have the ConfigureKestrel. However you can pass a KestrelServerOptions Action into UseKestrel instead which may give you the control your after. It overrides the settings from CreateDefaultBuilder.

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>().UseKestrel(options => options.Listen(IPAddress.Loopback,60000));

Incidentally I just found if you start with a plain .NetCore console app, then add...

    Microsoft.AspNetCore
    Microsoft.AspNetCore.Mvc

This gives you a (fairly minimalist) project running AspNetCore 2.2 instead of 2.1

Shawnshawna answered 20/6, 2019 at 12:44 Comment(1)
I'm kind of curious now... if you used the KestrelServerOptions Action with 2.2 would it win or would the values in the ConfigureKestrel Method win?Shawnshawna

© 2022 - 2024 — McMap. All rights reserved.