Rick Anderson 2019-01-31 09:40:50 -10:00 committed by GitHub
parent 4d8b6ca6a0
commit 687f240d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -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)]
<xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder> remains in 3.0 and is the type of the `webBuilder` seen in the preceding code sample. <xref:Microsoft.AspNetCore.Hosting.WebHostBuilder> 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 <xref:Microsoft.Extensions.Configuration.IConfiguration>, <xref:Microsoft.Extensions.Hosting.IHostingEnvironment>, and <xref:Microsoft.AspNetCore.Hosting.IHostingEnvironment> 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.

View File

@ -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<Startup>();
});
}
#endregion
}

View File

@ -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<Startup>();
}
#endregion
}