9
votes

What am I using:

  • .NET Core SDK 3.0.100
  • Visual Studio Community 2019 Version 16.3.2

I created a new ASP.NET Core Web API project targeting netcoreapp3.0 and I get the following error:

The type or namespace name 'CreateDefaultBuilder' does not exist in the namespace 'Template.Host' (are you missing an assembly reference?)

enter image description here

2

2 Answers

23
votes

Take another look at the error message:

The type or namespace name 'CreateDefaultBuilder' does not exist in the namespace 'Template.Host'...

When you write Host.CreateDefaultBuilder in a namespace of Template.Host, the compiler assumes you mean Template.Host.CreateDefaultBuilder.

There's a few options for fixing this:

  1. Nest the using statement inside of your namespace:

     namespace Template.Host
     {
         using Microsoft.Extensions.Hosting;
    
         // ...
     }
    
  2. Alias the Microsoft.Extensions.Hosting.Host type inside of your namespace:

     namespace Template.Host
     {
         using Host = Microsoft.Extensions.Hosting.Host;
    
         // ...
     }
    
  3. Use the fully qualified name for the Host type:

     Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
    

Host represents the Generic Host and is preferred over WebHost in ASP.NET Core 3.0+.

1
votes

UPDATE: I am of such a lowly SO status I can’t comment on Kirk’s post. I wasn’t aware of Host being the preferred in 3.0. Anyway, Kirk’s answer should be the correct one

You should be using WebHost (not Host) as follows:

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

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