Skip to content

Commit

Permalink
Update BuildWebHost to CreateWebHostBuilder (MD topics)
Browse files Browse the repository at this point in the history
Updates

Updates

Update

Tab update

Repair versioning
  • Loading branch information
guardrex committed Oct 27, 2018
1 parent 302034c commit 3bc2ed6
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 40 deletions.
109 changes: 87 additions & 22 deletions aspnetcore/fundamentals/host/web-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: guardrex
description: Learn about the web host in ASP.NET Core, which is responsible for app startup and lifetime management.
ms.author: riande
ms.custom: mvc
ms.date: 10/18/2018
ms.date: 10/27/2018
uid: fundamentals/host/web-host
---
# ASP.NET Core Web Host
Expand All @@ -15,7 +15,7 @@ ASP.NET Core apps configure and launch a *host*. The host is responsible for app

## Set up a host

::: moniker range=">= aspnetcore-2.0"
::: moniker range=">= aspnetcore-2.1"

Create a host using an instance of [IWebHostBuilder](/dotnet/api/microsoft.aspnetcore.hosting.iwebhostbuilder). This is typically performed in the app's entry point, the `Main` method. In the project templates, `Main` is located in *Program.cs*. A typical *Program.cs* calls [CreateDefaultBuilder](/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder) to start setting up a host:

Expand All @@ -33,6 +33,31 @@ public class Program
}
```

::: moniker-end

::: moniker range="= aspnetcore-2.0"

Create a host using an instance of [IWebHost](/dotnet/api/microsoft.aspnetcore.hosting.iwebhost). This is typically performed in the app's entry point, the `Main` method. In the project templates, `Main` is located in *Program.cs*. A typical *Program.cs* calls [CreateDefaultBuilder](/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder) to start setting up a host:

```csharp
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
```

::: moniker-end

::: moniker range=">= aspnetcore-2.0"

`CreateDefaultBuilder` performs the following tasks:

* Configures [Kestrel](xref:fundamentals/servers/kestrel) as the web server and configures the server using the app's hosting configuration providers. For the Kestrel default options, see <xref:fundamentals/servers/kestrel#kestrel-options>.
Expand Down Expand Up @@ -86,12 +111,11 @@ The configuration defined by `CreateDefaultBuilder` can be overridden and augmen
{
options.Limits.MaxRequestBodySize = 20000000;
});
...
```

::: moniker-end

::: moniker range="= aspnetcore-2.0 || aspnetcore-2.1"
::: moniker range="= aspnetcore-2.1"

* The following call to [UseKestrel](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderkestrelextensions.usekestrel) overrides the default [Limits.MaxRequestBodySize](/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserverlimits.maxrequestbodysize) of 30,000,000 bytes established when Kestrel was configured by `CreateDefaultBuilder`:

Expand All @@ -101,7 +125,21 @@ The configuration defined by `CreateDefaultBuilder` can be overridden and augmen
{
options.Limits.MaxRequestBodySize = 20000000;
});
...
```

::: moniker-end

::: moniker range="= aspnetcore-2.0"

* The following call to [UseKestrel](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderkestrelextensions.usekestrel) overrides the default [Limits.MaxRequestBodySize](/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserverlimits.maxrequestbodysize) of 30,000,000 bytes established when Kestrel was configured by `CreateDefaultBuilder`:

```csharp
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 20000000;
})
.Build();
```

::: moniker-end
Expand All @@ -119,22 +157,7 @@ For more information on app configuration, see <xref:fundamentals/configuration/

::: moniker range="< aspnetcore-2.0"

Create a host using an instance of [WebHostBuilder](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder). Creating a host is typically performed in the app's entry point, the `Main` method. In the project templates, `Main` is located in *Program.cs*:

```csharp
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
```
Create a host using an instance of [WebHostBuilder](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilder). Creating a host is typically performed in the app's entry point, the `Main` method.

`WebHostBuilder` requires a [server that implements IServer](xref:fundamentals/servers/index). The built-in servers are [Kestrel](xref:fundamentals/servers/kestrel) and [HTTP.sys](xref:fundamentals/servers/httpsys) (prior to the release of ASP.NET Core 2.0, HTTP.sys was called [WebListener](xref:fundamentals/servers/weblistener)). In this example, the [UseKestrel extension method](/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderkestrelextensions.usekestrel?view=aspnetcore-1.1) specifies the Kestrel server.

Expand Down Expand Up @@ -545,7 +568,7 @@ var host = new WebHostBuilder()

Use [Configuration](xref:fundamentals/configuration/index) to configure the web host. In the following example, host configuration is optionally specified in a *hostsettings.json* file. Any configuration loaded from the *hostsettings.json* file may be overridden by command-line arguments. The built configuration (in `config`) is used to configure the host with [UseConfiguration](/dotnet/api/microsoft.aspnetcore.hosting.hostingabstractionswebhostbuilderextensions.useconfiguration). `IWebHostBuilder` configuration is added to the app's configuration, but the converse isn't true&mdash;`ConfigureAppConfiguration` doesn't affect the `IWebHostBuilder` configuration.

::: moniker range=">= aspnetcore-2.0"
::: moniker range=">= aspnetcore-2.1"

Overriding the configuration provided by `UseUrls` with *hostsettings.json* config first, command-line argument config second:

Expand All @@ -558,6 +581,48 @@ public class Program
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hostsettings.json", optional: true)
.AddCommandLine(args)
.Build();

return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.Configure(app =>
{
app.Run(context =>
context.Response.WriteAsync("Hello, World!"));
});
}
}
```

*hostsettings.json*:

```json
{
urls: "http://*:5005"
}
```

::: moniker-end

::: moniker range="= aspnetcore-2.0"

Overriding the configuration provided by `UseUrls` with *hostsettings.json* config first, command-line argument config second:

```csharp
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand Down
Loading

0 comments on commit 3bc2ed6

Please sign in to comment.