Search code examples
c#.net-core-3.1

Building this implementation of IWebHostBuilder is not supported in ASP.NET Core 3.1


I am trying upgrade my .net core 2.2 to 3.1 and getting following error.

Building this implementation of IWebHostBuilder is not supported.

EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.

I am not sure why this is complaining about IWebHostBuilder.

Can someone please guide me.

Program.cs

        public static void Main(string[] args)
                {
                     IHost webHost = (IHost)BuildWebHost(args);
                      var runTask = webHost.RunAsync();
                            runTask.Wait();
                            return;          
                }
         
                     
public static IHostBuilder BuildWebHost(string[] args) =>
                        Host.CreateDefaultBuilder(args)
                        .ConfigureWebHostDefaults
                        (Web =>
                        {
                            Web.UseStartup<Startup>()
                              .UseConfiguration(Configuration)
                              .UseSerilog()
                              .Build();
                        });

Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");               
                app.UseHsts();
            }
            
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseHttpContext();
          
            app.UseEndpoints(endpoints =>
                        endpoints.MapControllers());           
        }

Solution

  • This

    //...
    
       (Web =>
        {
            Web.UseStartup<Startup>()
              .UseConfiguration(Configuration)
              .UseSerilog()
              .Build(); //<--DON'T DO THIS
        });
    

    is causing the original problem because of

    //...GenericWebHostBuilder
    
    public IWebHost Build()
    {
        throw new NotSupportedException($"Building this implementation of {nameof(IWebHostBuilder)} is not supported.");
    }
    

    Source

    The code needs to be refactored to

    public static async Task Main(string[] args) {
         IHost host = BuildWebHost(args);
         await host.RunAsync();  
    }
                     
    public static IHost BuildWebHost(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(Web => {
                Web.UseStartup<Startup>()
                  .UseConfiguration(Configuration)
                  .UseSerilog();
            })
            .Build();
    

    In order to follow the suggested format from documentation.

    If there is no need for the code to be async then this will work as well

    public static void Main(string[] args) {
        IHost host = BuildWebHost(args);
        host.Run();  
    }