From afacb05b06ffa06557a9c811372d399e43486e0b Mon Sep 17 00:00:00 2001 From: catdemon Date: Mon, 1 Jul 2019 23:10:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?autofac=E9=80=9A=E8=BF=87type=E8=BF=9B?= =?UTF-8?q?=E5=85=A5=E6=B3=A8=E5=86=8C=20autofac=E9=80=9A=E8=BF=87Assembly?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 27 +++++++- .../AutofacExtensions.cs | 65 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db6117e..9819deb 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ public interface IValuesApi : IHttpApi ITask GetAsync(int id); } ``` -> 注册和配置接口 +> 使用泛型注册和配置接口 + ```c# var builder = new ContainerBuilder(); builder.RegisterHttpApi().ConfigureHttpApiConfig(c => @@ -33,6 +34,30 @@ builder.RegisterHttpApi().ConfigureHttpApiConfig(c => }); ``` +> 使用Type注册和配置接口 + +```c# +var builder = new ContainerBuilder(); +builder.RegisterApiByType(typeof(IValuesApi),c => +{ + c.HttpHost = new Uri("http://localhost:9999/"); + c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; +}); +``` +注:type必须继承于IHttpApi +> 使用Assembly注册和配置接口 + +```c# +var assemblyArray = new Assembly[] { Assembly.GetExecutingAssembly() }; +var builder = new ContainerBuilder(); +builder.RegisterApiByAssembly(assemblyArray,c => +{ + c.HttpHost = new Uri("http://localhost:9999/"); + c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; +}); +``` + +通过反射扫描所有继承了IHttpApi的接口,把所有扫描到的接口注册到容器中 ### 1 DependencyInjection扩展 #### 1.1 Nuget diff --git a/WebApiClient.Extensions.Autofac/AutofacExtensions.cs b/WebApiClient.Extensions.Autofac/AutofacExtensions.cs index ec4d76e..bc6e99b 100644 --- a/WebApiClient.Extensions.Autofac/AutofacExtensions.cs +++ b/WebApiClient.Extensions.Autofac/AutofacExtensions.cs @@ -1,5 +1,8 @@  using Autofac; +using System; +using System.Linq; +using System.Reflection; namespace WebApiClient.Extensions.Autofac { @@ -8,6 +11,16 @@ namespace WebApiClient.Extensions.Autofac /// public static class AutofacExtensions { + /// + /// RegisterHttpApi方法名称 + /// + private const string registerHttpApiMethName = "RegisterHttpApi"; + + /// + /// ConfigureHttpApiConfig方法名称 + /// + private const string configureHttpApiConfigMethName = "ConfigureHttpApiConfig"; + /// /// 注册HttpApi /// 返回HttpApi工厂创建器 @@ -20,5 +33,57 @@ public static HttpApiFactoryBuilder RegisterHttpApi(this { return new HttpApiFactoryBuilder(builder); } + + /// + /// 通过Type类型注册到autofa + /// + /// + /// 接口类型 + /// 配置项 + public static void RegisterApiByType(this ContainerBuilder builder,Type type, Action 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) }); + configMethon.Invoke(configureHttpApiConfig, new[] { configOptions }); + } + + /// + /// 通过程序集注册类型 + /// + /// + /// + /// + public static void RegisterApiByAssembly(this ContainerBuilder builder, Assembly assembly, Action configOptions) + { + builder.RegisterApiByAssembly(new Assembly[] { assembly }, configOptions); + } + + /// + /// 通过程序集注册类型 + /// + /// + /// + /// + public static void RegisterApiByAssembly(this ContainerBuilder builder,Assembly[] assembliesArray, Action configOptions) + { + foreach(var assemblies in assembliesArray) + { + registerApiByAssembly(builder, assemblies, configOptions); + } + } + + private static void registerApiByAssembly(ContainerBuilder builder,Assembly assemblies, Action configOptions) + { + var httpApiType = typeof(IHttpApi); + + var httpApiList = assemblies.GetTypes().Where(p => httpApiType.IsAssignableFrom(p)).ToList(); + + foreach (var httpApi in httpApiList) + { + builder.RegisterApiByType(httpApi, configOptions); + } + } } } From c03e292c2b3cc63ca5310c89de90deb00c1d7bf7 Mon Sep 17 00:00:00 2001 From: cheetahing <2894339590@qq.com> Date: Mon, 1 Jul 2019 23:13:51 +0800 Subject: [PATCH 2/2] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RegisterApiByAssembly增加重载 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9819deb..01f61a0 100644 --- a/README.md +++ b/README.md @@ -48,16 +48,16 @@ builder.RegisterApiByType(typeof(IValuesApi),c => > 使用Assembly注册和配置接口 ```c# -var assemblyArray = new Assembly[] { Assembly.GetExecutingAssembly() }; +var assembly = Assembly.GetExecutingAssembly(); var builder = new ContainerBuilder(); -builder.RegisterApiByAssembly(assemblyArray,c => +builder.RegisterApiByAssembly(assembly,c => { c.HttpHost = new Uri("http://localhost:9999/"); c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; }); ``` -通过反射扫描所有继承了IHttpApi的接口,把所有扫描到的接口注册到容器中 +通过反射扫描程序集中所有继承了IHttpApi的接口,把所有扫描到的接口注册到容器中 ### 1 DependencyInjection扩展 #### 1.1 Nuget