43

I'm trying to use the new GenericHost in .NET Core 3.0 documented here but I'm getting a really basic error that stats IHostBuilder does not contain a definition for the ConfigureWebHostDefaults function.

Looking at the ASP.NET 3.0 documentation here for the IHostBuilder interface here I cant see any reference to ConfigureWebHostDefaults so I'm a bit confused.

I'm using the 3.0.0 packages for Microsoft.Extensions.Hosting and Microsoft.Extensions.Hosting.Abstractions but cant help but feel I'm either missing something really obvious or that ConfigureWebHostDefaults has for some reason been removed?

Update -- Screen shot of Program.cs

enter image description here

Update -- .csproj file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
  </ItemGroup>

</Project>
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
Tim
  • 495
  • 1
  • 4
  • 10
  • 6
    @Tim post your code, not images of your code. Images don't compile, they can't be copied and tested. – Panagiotis Kanavos Sep 25 '19 at 12:02
  • Yeah remote access isn't going to happen sorry @TanvirArjel. – Tim Sep 25 '19 at 12:05
  • @PanagiotisKanavos I can share the .csproj but its pretty much the template console application with the additional package references. Will add it to the question in any case as it might be useful – Tim Sep 25 '19 at 12:07

1 Answers1

72

Along with using Microsoft.Extensions.Hosting, ConfigureWebHostDefaults also require using Microsoft.AspNetCore.Hosting; as follows:

using Microsoft.AspNetCore.Hosting; //<-- Here it is
using Microsoft.Extensions.Hosting;

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

        public static IHostBuilder CreateWebHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

Moreover it look like your project is a Console Application. That is the problem. ConfigureWebHostDefaults is for Web Application only. So you can convert your Console Application into Web Applicaton by replacing Sdk="Microsoft.NET.Sdk" with Sdk="Microsoft.NET.Sdk.Web" in your .csproj file as follows:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

</Project>
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • 3
    Yep completely overlooked that - I've updated it to use Sdk.Web and its picking up the ConfigureWebHostDefaults extension. Thanks for the help – Tim Sep 25 '19 at 12:24
  • @Tim Great to see that your problem has been solved. Welcome. – TanvirArjel Sep 25 '19 at 12:25
  • Thank you @TanvirArjel for the solution. It didn't mention anywhere in Microsoft's 3.0 migration documentation(https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio-code#hostbuilder-replaces-webhostbuilder) that both namespaces should be used together. Keyword (*.Hosting) in namespaces can miss lead anyone. – Dharmendra Kumar Mistry Sep 26 '19 at 08:31
  • I want to start SignalR server along with my startup of WPF application. I have used this code. works perfectly fine. But I need to keep a empty web project in the solution so that ConfigureWebHostDefaults line does not throw exception. Any other option to make this line work and get rid of the web project. – Swapnil Mahajan Sep 29 '22 at 08:51
  • Too bad, it still doesn't work. They've made a mess of it. – Jeroen van Langen Jul 05 '23 at 13:03