This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathWebHostFactoryResolver.cs
62 lines (55 loc) · 2.92 KB
/
WebHostFactoryResolver.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
// 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 System.Reflection;
namespace Microsoft.AspNetCore.Hosting.WebHostBuilderFactory
{
internal class WebHostFactoryResolver
{
public static readonly string CreateWebHostBuilder = nameof(CreateWebHostBuilder);
public static readonly string BuildWebHost = nameof(BuildWebHost);
public static FactoryResolutionResult<TWebhost,TWebhostBuilder> ResolveWebHostBuilderFactory<TWebhost, TWebhostBuilder>(Assembly assembly)
{
var programType = assembly?.EntryPoint?.DeclaringType;
if (programType == null)
{
return FactoryResolutionResult<TWebhost, TWebhostBuilder>.NoEntryPoint();
}
var factory = programType?.GetTypeInfo().GetDeclaredMethod(CreateWebHostBuilder);
if (factory == null)
{
return FactoryResolutionResult<TWebhost, TWebhostBuilder>.NoCreateWebHostBuilder(programType);
}
return FactoryResolutionResult<TWebhost, TWebhostBuilder>.Succeded(args => (TWebhostBuilder)factory.Invoke(null, new object[] { args }), programType);
}
public static FactoryResolutionResult<TWebhost, TWebhostBuilder> ResolveWebHostFactory<TWebhost, TWebhostBuilder>(Assembly assembly)
{
// We want to give priority to BuildWebHost over CreateWebHostBuilder for backwards
// compatibility with existing projects that follow the old pattern.
var findResult = ResolveWebHostBuilderFactory<TWebhost, TWebhostBuilder>(assembly);
switch (findResult.ResultKind)
{
case FactoryResolutionResultKind.NoEntryPoint:
return findResult;
case FactoryResolutionResultKind.Success:
case FactoryResolutionResultKind.NoCreateWebHostBuilder:
var buildWebHostMethod = findResult.ProgramType.GetTypeInfo().GetDeclaredMethod("BuildWebHost");
if (buildWebHostMethod == null)
{
if (findResult.ResultKind == FactoryResolutionResultKind.Success)
{
return findResult;
}
return FactoryResolutionResult<TWebhost, TWebhostBuilder>.NoBuildWebHost(findResult.ProgramType);
}
else
{
return FactoryResolutionResult<TWebhost, TWebhostBuilder>.Succeded(args => (TWebhost)buildWebHostMethod.Invoke(null, new object[] { args }), findResult.ProgramType);
}
case FactoryResolutionResultKind.NoBuildWebHost:
default:
throw new InvalidOperationException();
}
}
}
}