Last active
December 2, 2016 14:59
-
-
Save AndSDev/03b2ed680ded355724bef4f7e02df491 to your computer and use it in GitHub Desktop.
ASP.NET Core 1.1. Use the given configuration settings on the web host. Compatible with the configuration section. Fix https://github.com/aspnet/Hosting/issues/839
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
namespace Microsoft.AspNetCore.Hosting | |
{ | |
public static class WebHostConfigurationSection | |
{ | |
/// <summary> | |
/// Use the given configuration settings on the web host. Compatible with the configuration section. | |
/// </summary> | |
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param> | |
/// <param name="configuration">The <see cref="IConfiguration"/> containing settings to be used.</param> | |
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> | |
public static IWebHostBuilder UseConfigurationSection(this IWebHostBuilder hostBuilder, IConfiguration configuration) | |
{ | |
foreach (var setting in configuration.AsEnumerable(true)) | |
{ | |
hostBuilder.UseSetting(setting.Key, setting.Value); | |
} | |
return hostBuilder; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
namespace WebApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var config = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: true) | |
.Build(); | |
var host = new WebHostBuilder() | |
.UseConfigurationSection(config.GetSection("kestrel")) | |
.UseKestrel() | |
.UseContentRoot(Directory.GetCurrentDirectory()) | |
.UseIISIntegration() | |
.UseStartup<Startup>() | |
.Build(); | |
host.Run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment