This repository has been archived by the owner on Nov 21, 2018. It is now read-only.
/
Program.cs
92 lines (80 loc) · 4.02 KB
/
Program.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HostFiltering;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace CreateDefaultBuilderOfTApp
{
public class Program
{
static void Main(string[] args)
{
string responseMessage = null;
WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
.ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context))
.ConfigureKestrel(options => options
.Configure(options.ConfigurationLoader.Configuration)
.Endpoint("HTTP", endpointOptions =>
{
if (responseMessage == null
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
{
responseMessage = "Default Kestrel configuration not read.";
}
}))
.Configure(app => app.Run(context =>
{
// Verify allowed hosts were loaded
var hostFilteringOptions = app.ApplicationServices.GetRequiredService<IOptions<HostFilteringOptions>>();
var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts);
if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal))
{
responseMessage = "AllowedHosts not loaded into Options.";
}
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}))
.Build()
.Run();
}
private static string GetResponseMessage(WebHostBuilderContext context)
{
// Verify ContentRootPath set
var contentRoot = Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT");
if (!string.Equals(contentRoot, context.HostingEnvironment.ContentRootPath, StringComparison.Ordinal))
{
return $"ContentRootPath incorrect. Expected: {contentRoot} Actual: {context.HostingEnvironment.ContentRootPath}";
}
// Verify appsettings.json loaded
if (!string.Equals("settingsValue", context.Configuration["settingsKey"], StringComparison.Ordinal))
{
return $"appsettings.json not loaded into Configuration.";
}
// Verify appsettings.environment.json loaded
if (!string.Equals("devSettingsValue", context.Configuration["devSettingsKey"], StringComparison.Ordinal))
{
return $"appsettings.{context.HostingEnvironment.EnvironmentName}.json not loaded into Configuration.";
}
// TODO: Verify UserSecrets loaded
// Verify environment variables loaded
if (!string.Equals("envValue", context.Configuration["envKey"], StringComparison.Ordinal))
{
return $"Environment variables not loaded into Configuration.";
}
// Verify command line arguments loaded
if (!string.Equals("cliValue", context.Configuration["cliKey"], StringComparison.Ordinal))
{
return $"Command line arguments not loaded into Configuration.";
}
// TODO: Verify AddConsole called
// TODO: Verify AddDebug called
// TODO: Verify UseIISIntegration called
return null;
}
}
}