/
HostingAbstractionsHostBuilderExtensions.cs
34 lines (31 loc) · 1.33 KB
/
HostingAbstractionsHostBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Hosting
{
public static class HostingAbstractionsHostBuilderExtensions
{
/// <summary>
/// Builds and starts the host.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder"/> to start.</param>
/// <returns>The started <see cref="IHost"/>.</returns>
public static IHost Start(this IHostBuilder hostBuilder)
{
return hostBuilder.StartAsync().GetAwaiter().GetResult();
}
/// <summary>
/// Builds and starts the host.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder"/> to start.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the start.</param>
/// <returns>The started <see cref="IHost"/>.</returns>
public static async Task<IHost> StartAsync(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default)
{
IHost host = hostBuilder.Build();
await host.StartAsync(cancellationToken).ConfigureAwait(false);
return host;
}
}
}