Friday, April 24, 2020

How do I make Program.cs use Kestrel in modern .NET Core 3+ times?

My code:

using System.Diagnostics.CodeAnalysis;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Yin
{
   public class Program
   {
      [ExcludeFromCodeCoverage]
      public static void Main(string[] args)
      {
         CreateHostBuilder(args).Build().Run();
      }
      
      [ExcludeFromCodeCoverage]
      public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureWebHostDefaults(webBuilder =>
            {
               webBuilder.UseStartup<Startup>().UseKestrel();
            });
   }
}

 
 

A coworker's code:

namespace Yin
{
   using System;
   using System.Diagnostics.CodeAnalysis;
   using Yang;
   using Microsoft.AspNetCore;
   using Microsoft.AspNetCore.Hosting;
   using Serilog;
   
   [ExcludeFromCodeCoverage]
   public class Program
   {
      [ExcludeFromCodeCoverage]
      public static int Main(string[] args)
      {
         Logger.CreateLogger();
         
         try
         {
            Log.Information("{message}", "Starting web host: FnDocs.Web.Api");
            CreateWebHostBuilder(args).Build().Run();
            return 0;
         }
         catch (Exception ex)
         {
            Log.Error(ex, "Host terminated unexpectedly{ex}");
            return 1;
         }
      }
      
      [ExcludeFromCodeCoverage]
      public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel()
            .UseSerilog();
   }
}

No comments:

Post a Comment