Skip to content

Instantly share code, notes, and snippets.

@migajek
Created December 20, 2019 11:14
Show Gist options
  • Save migajek/5feb5cbc940c86cc009f5ca1c9fcc278 to your computer and use it in GitHub Desktop.
Save migajek/5feb5cbc940c86cc009f5ca1c9fcc278 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace DotnetConfigRepro
{
class Repro
{
public static async Task CheckConfigureAppConfig(string[] args)
{
var builder = Host
.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((_, cb) => CustomizeConfigurationBuilder(cb))
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder
.UseStartup<Startup>()
.UseUrls("http://localhost:9542")
);
await builder.RunAndWaitTillCompletion();
}
public static async Task CheckUseConfiguratio(string[] args)
{
var cfgBuilder = new ConfigurationBuilder();
CustomizeConfigurationBuilder(cfgBuilder);
var config = cfgBuilder.Build();
var builder = Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder
.UseStartup<Startup>()
.UseConfiguration(config)
.UseUrls("http://localhost:9541")
);
await builder.RunAndWaitTillCompletion();
}
private static void CustomizeConfigurationBuilder(IConfigurationBuilder cb) =>
cb
.AddJsonFile("appsettings.json")
.AddInMemoryCollection(new Dictionary<string, string>()
{
["Value"] = "from memory provider"
});
}
class Program
{
static async Task Main(string[] args)
{
// both cases should have the same configuration and output "from memory provider" to debug output.
// this is not the case.
await Repro.CheckConfigureAppConfig(args);
await Repro.CheckUseConfiguratio(args);
}
}
class Startup
{
public Startup(IConfiguration config)
{
var val = config.GetValue<string>("Value");
WriteLine($"Value: {val}");
ShowProvidersList(config);
// mark done.
HostLifetimeControl.StopMe();
}
private static void ShowProvidersList(IConfiguration config)
{
if (!(config is ConfigurationRoot root)) return;
WriteLine("Providers: ");
foreach (var provider in root.Providers)
{
WriteLine($"\t - {provider}");
}
}
private static void WriteLine(string s) => Debug.WriteLine(s);
public void ConfigureServices(IServiceCollection serviceCollection) { }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { }
}
// helper meat
internal static class HostLifetimeControl
{
private static readonly AsyncLocal<TaskCompletionSource<int>> _cts = new AsyncLocal<TaskCompletionSource<int>>();
public static async Task RunAndWaitTillCompletion(this IHostBuilder builder)
{
_cts.Value = new TaskCompletionSource<int>();
var host = builder.Build();
host.RunAsync();
await _cts.Value.Task.ContinueWith(_ =>
host.StopAsync());
}
public static void StopMe() => _cts.Value.SetResult(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment