Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.0: The type or namespace name 'CreateDefaultBuilder' does not exist in the namespace

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

like image 434
Chris Avatar asked Oct 05 '19 14:10

Chris


1 Answers

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+.

like image 60
Kirk Larkin Avatar answered Sep 24 '22 17:09

Kirk Larkin