-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAutofacExtensions.cs
More file actions
89 lines (79 loc) · 3.38 KB
/
AutofacExtensions.cs
File metadata and controls
89 lines (79 loc) · 3.38 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
using Autofac;
using System;
using System.Linq;
using System.Reflection;
namespace WebApiClient.Extensions.Autofac
{
/// <summary>
/// 基于Autofac的扩展
/// </summary>
public static class AutofacExtensions
{
/// <summary>
/// RegisterHttpApi方法名称
/// </summary>
private const string registerHttpApiMethName = "RegisterHttpApi";
/// <summary>
/// ConfigureHttpApiConfig方法名称
/// </summary>
private const string configureHttpApiConfigMethName = "ConfigureHttpApiConfig";
/// <summary>
/// 注册HttpApi
/// 返回HttpApi工厂创建器
/// </summary>
/// <typeparam name="TInterface">接口类型</typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static HttpApiFactoryBuilder<TInterface> RegisterHttpApi<TInterface>(this ContainerBuilder builder)
where TInterface : class, IHttpApi
{
return new HttpApiFactoryBuilder<TInterface>(builder);
}
/// <summary>
/// 通过Type类型注册到autofa
/// </summary>
/// <param name="builder"></param>
/// <param name="type">接口类型</param>
/// <param name="configOptions">配置项</param>
public static void RegisterApiByType(this ContainerBuilder builder,Type type, Action<HttpApiConfig> configOptions)
{
var registerApi = typeof(WebApiClient.Extensions.Autofac.AutofacExtensions).GetMethod(registerHttpApiMethName).MakeGenericMethod(type);
var configureHttpApiConfig = registerApi.Invoke(null, new[] { builder });
var configMethon = configureHttpApiConfig.GetType().GetMethod(configureHttpApiConfigMethName, new[] { typeof(System.Action<WebApiClient.HttpApiConfig>) });
configMethon.Invoke(configureHttpApiConfig, new[] { configOptions });
}
/// <summary>
/// 通过程序集注册类型
/// </summary>
/// <param name="builder"></param>
/// <param name="assembly"></param>
/// <param name="configOptions"></param>
public static void RegisterApiByAssembly(this ContainerBuilder builder, Assembly assembly, Action<HttpApiConfig> configOptions)
{
builder.RegisterApiByAssembly(new Assembly[] { assembly }, configOptions);
}
/// <summary>
/// 通过程序集注册类型
/// </summary>
/// <param name="builder"></param>
/// <param name="assembliesArray"></param>
/// <param name="configOptions"></param>
public static void RegisterApiByAssembly(this ContainerBuilder builder,Assembly[] assembliesArray, Action<HttpApiConfig> configOptions)
{
foreach(var assemblies in assembliesArray)
{
registerApiByAssembly(builder, assemblies, configOptions);
}
}
private static void registerApiByAssembly(ContainerBuilder builder,Assembly assemblies, Action<HttpApiConfig> configOptions)
{
var httpApiType = typeof(IHttpApi);
var httpApiList = assemblies.GetTypes().Where(p => httpApiType.IsAssignableFrom(p)).ToList();
foreach (var httpApi in httpApiList)
{
builder.RegisterApiByType(httpApi, configOptions);
}
}
}
}