Skip to content

Instantly share code, notes, and snippets.

@AndSDev
Last active December 2, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndSDev/03b2ed680ded355724bef4f7e02df491 to your computer and use it in GitHub Desktop.
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
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;
}
}
}
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