forked from lord-executor/Ninject.Web.AspNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (82 loc) · 3.04 KB
/
Program.cs
File metadata and controls
96 lines (82 loc) · 3.04 KB
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
93
94
95
96
using Microsoft.AspNetCore.Hosting;
using Ninject;
using Ninject.Web.AspNetCore.Hosting;
using Ninject.Web.Common.SelfHost;
using System;
using System.Linq;
namespace SampleApplication_AspNetCore
{
public class Program
{
public static void Main(string[] args)
{
// Simple (and probably unreliable) IIS detection mechanism
var model = Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES") == "Microsoft.AspNetCore.Server.IISIntegration" ? "IIS" : null;
// The hosting model can be explicitly configured with the SERVER_HOSTING_MODEL environment variable.
// See https://www.andrecarlucci.com/en/setting-environment-variables-for-asp-net-core-when-publishing-on-iis/ for
// setting the variable in IIS.
model = Environment.GetEnvironmentVariable("SERVER_HOSTING_MODEL") ?? model;
// Command line arguments have higher precedence than environment variables
model = args.FirstOrDefault(arg => arg.StartsWith("--use"))?.Substring(5) ?? model;
var hostConfiguration = new AspNetCoreHostConfiguration(args)
.UseStartup<Startup>()
.UseWebHostBuilder(CreateWebHostBuilder)
.BlockOnStart();
switch (model)
{
case "Kestrel":
hostConfiguration.UseKestrel();
break;
case "HttpSys":
hostConfiguration.UseHttpSys();
break;
case "IIS":
hostConfiguration.UseIIS();
break;
case "IISExpress":
// Yes, _this_ is actually a "thing"...
// The netstandard2.0 version of ASP.NET Core 2.2 works quite different when it comes to IISExpress integration
// than its .NET Core counterpart.
// * In a .NET 4.8 project, you MUST to UseKestrel when running in IISExpress
// * In a .NET Core project (tested in 2.2 and 3.1), you MUST to UseIIS when running in IISExpress
//
// ... This just makes my brain hurt.
if (IsDotNetCoreRuntime())
{
hostConfiguration.UseIIS();
}
else
{
hostConfiguration.UseKestrel();
}
break;
default:
throw new ArgumentException($"Unknown hosting model '{model}'");
}
var host = new NinjectSelfHostBootstrapper(CreateKernel, hostConfiguration);
host.Start();
}
public static IKernel CreateKernel()
{
var settings = new NinjectSettings();
// Unfortunately, in .NET Core projects, referenced NuGet assemblies are not copied to the output directory
// in a normal build which means that the automatic extension loading does not work _reliably_ and it is
// much more reasonable to not rely on that and load everything explicitly.
settings.LoadExtensions = false;
var kernel = new StandardKernel(settings);
kernel.Load(typeof(AspNetCoreHostConfiguration).Assembly);
return kernel;
}
public static IWebHostBuilder CreateWebHostBuilder()
{
return new DefaultWebHostConfiguration(null)
.ConfigureAll()
.GetBuilder()
.UseWebRoot(@"..\SampleApplication-Shared\wwwroot");
}
public static bool IsDotNetCoreRuntime()
{
return System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.Contains(".NET Core");
}
}
}