Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[WebApplication1.Startup]'

前端 未结 5 2278
轮回少年
轮回少年 2021-02-05 12:29

I created an ASP.NET Core 3.0 Web Application with the default template in Visual Studio 2019 Preview 2.2 and tried to inject an ILogger in Startup:

namespace We         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 12:50

    This issue is related with IHostBuilder.

    For a temp workaround, I suggest you try IWebHost to replace IHostBuilder.

    Change your Program.cs like

    public class Program
    {
        public static void Main(string[] args)
        {           
            BuildWebHost(args).Run();
        }
        public static IWebHost BuildWebHost(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup()
                    .ConfigureLogging(logging =>
                    {
                        logging.ClearProviders();
                        logging.AddConsole();
                    })
                    .Build();      
    }
    

提交回复
热议问题