-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathConfigureServicesBuilder.cs
More file actions
68 lines (57 loc) · 2.28 KB
/
ConfigureServicesBuilder.cs
File metadata and controls
68 lines (57 loc) · 2.28 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
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
namespace Orleans.Runtime.Startup
{
internal class ConfigureServicesBuilder
{
public ConfigureServicesBuilder(MethodInfo configureServices)
{
if (configureServices == null)
{
throw new ArgumentNullException(nameof(configureServices));
}
// Only support IServiceCollection parameters
var parameters = configureServices.GetParameters();
if (parameters.Length > 1 ||
parameters.Any(p => p.ParameterType != typeof(IServiceCollection)))
{
throw new InvalidOperationException("ConfigureServices can take at most a single IServiceCollection parameter.");
}
MethodInfo = configureServices;
}
public IServiceProvider Build (object instance, IServiceCollection services)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return Invoke(instance, services);
}
private IServiceProvider Invoke(object instance, IServiceCollection exportServices)
{
var parameters = new object[MethodInfo.GetParameters().Length];
// Ctor ensures we have at most one IServiceCollection parameter
if (parameters.Length > 0)
{
parameters[0] = exportServices;
}
//
// For Orleans we've a a modified behavior, different from Asp.Net vNext, since Orleans will not fallback to
// default DI implementation if the ConfigureServices method is not returning a build DI container.
//
var serviceProvider = MethodInfo.Invoke(instance, parameters) as IServiceProvider;
if (serviceProvider == null)
{
throw new InvalidOperationException("The ConfigureServices method did not returned a configured IServiceProvider instance.");
}
return serviceProvider;
}
public MethodInfo MethodInfo { get; }
}
}