diff --git a/aspnetcore/migration/22-to-30.md b/aspnetcore/migration/22-to-30.md index 209218f125..50cc4d93c7 100644 --- a/aspnetcore/migration/22-to-30.md +++ b/aspnetcore/migration/22-to-30.md @@ -69,3 +69,22 @@ Newtonsoft settings can be set with `AddNewtonsoftJson`: options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()); ``` + +## HostBuilder replaces WebHostBuilder + +The ASP.NET Core 3.0 templates use [Generic Host](xref:fundamentals/host/generic-host). Previous versions used [Web Host](xref:fundamentals/host/web-host). The following code shows the ASP.NET Core 3.0 template generated `Program` class: + +[!code-csharp[](22-to-30/samples/Program.cs?name=snippet)] + +The following code shows the ASP.NET Core 2.2 template-generated `Program` class: + +[!code-csharp[](22-to-30/samples/Program2.2.cs?name=snippet)] + + remains in 3.0 and is the type of the `webBuilder` seen in the preceding code sample. will be deprecated in a future release and replaced by `HostBuilder`. + +## Moving from WebHostBuilder to HostBuilder + +The most significant change from `WebHostBuilder` to `HostBuilder` is in [dependency injection (DI)](xref:fundamentals/dependency-injection). When using `HostBuilder`, you can only inject , , and into Startup's constructor. The `HostBuilder` DI constraints: + +* Enable the DI container to be built only one time. +* Avoids the resulting object lifetime issues like resolving multiple instances of singletons. \ No newline at end of file diff --git a/aspnetcore/migration/22-to-30/samples/Program.cs b/aspnetcore/migration/22-to-30/samples/Program.cs new file mode 100644 index 0000000000..75debbdb02 --- /dev/null +++ b/aspnetcore/migration/22-to-30/samples/Program.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace aspnetcoreapp +{ + #region snippet + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } + #endregion +} diff --git a/aspnetcore/migration/22-to-30/samples/Program2.2.cs b/aspnetcore/migration/22-to-30/samples/Program2.2.cs new file mode 100644 index 0000000000..976b772842 --- /dev/null +++ b/aspnetcore/migration/22-to-30/samples/Program2.2.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; + +namespace WebAPI +{ + #region snippet + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } + #endregion +} \ No newline at end of file