From d9cf5c7d220702195548feff202b34df3001fafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 18 Sep 2017 11:34:26 +0300 Subject: [PATCH] Created ServiceCollectionDynamicHttpClientProxyExtensions.AddHttpClientProxies. --- ...lectionDynamicHttpClientProxyExtensions.cs | 25 +++++++++++++++++-- .../DynamicProxying/ApiDescriptionFinder.cs | 2 ++ .../DynamicHttpProxyInterceptor.cs | 5 ---- src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs | 2 +- .../InternalServiceCollectionExtensions.cs | 11 ++++++-- .../Volo/Abp/Reflection/AssemblyFinder.cs | 3 +-- .../Volo/Abp/Reflection/TypeFinder.cs | 11 +++----- .../Volo/Abp/Http/AbpHttpTestModule.cs | 15 ++++------- .../Volo/Abp/TestApp/TestAppModule.cs | 1 - .../Volo/Abp/Reflection/TypeFinder_Tests.cs | 6 ++--- 10 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs b/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs index 17f5d51f7a..a2795d77c1 100644 --- a/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs +++ b/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs @@ -1,5 +1,8 @@ using System; +using System.Linq; +using System.Reflection; using Castle.DynamicProxy; +using Volo.Abp.Application.Services; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.Http.Client; using Volo.Abp.Http.Client.DynamicProxying; @@ -11,7 +14,25 @@ namespace Microsoft.Extensions.DependencyInjection { private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator(); - //TODO: AddHttpClientProxies for adding all services from single assembly! + public static IServiceCollection AddHttpClientProxies( + this IServiceCollection services, + Assembly assembly, + string baseUrl, + string moduleName = ModuleApiDescriptionModel.DefaultServiceModuleName) + { + //TODO: Add option to change type filter + + var serviceTypes = assembly.GetTypes().Where(t => + t.IsInterface && t.IsPublic && typeof(IApplicationService).IsAssignableFrom(t) + ); + + foreach (var serviceType in serviceTypes) + { + services.AddHttpClientProxy(serviceType, baseUrl, moduleName); + } + + return services; + } public static IServiceCollection AddHttpClientProxy(this IServiceCollection services, string baseUrl, string moduleName = ModuleApiDescriptionModel.DefaultServiceModuleName) { @@ -34,7 +55,7 @@ namespace Microsoft.Extensions.DependencyInjection serviceProvider => ProxyGeneratorInstance .CreateInterfaceProxyWithoutTarget( type, - (IInterceptor) serviceProvider.GetRequiredService(interceptorAdapterType) + (IInterceptor)serviceProvider.GetRequiredService(interceptorAdapterType) ) ); } diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs index 4ad80a516c..90eee9ae9f 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs @@ -19,6 +19,8 @@ namespace Volo.Abp.Http.Client.DynamicProxying { var apiDescription = await _descriptionCache.GetAsync(proxyConfig.BaseUrl); + //TODO: Cache finding? + var methodParameters = method.GetParameters().ToArray(); foreach (var module in apiDescription.Modules.Values) diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index c18ed8126f..09dadfe8ac 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -1,14 +1,9 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; -using System.Net.Http.Headers; -using System.Net.Mime; using System.Reflection; -using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; using Volo.Abp.DynamicProxy; using Volo.Abp.Http.Modeling; diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs index 864a49b6aa..a969b2ab97 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs @@ -34,7 +34,7 @@ namespace Volo.Abp services.AddSingleton(this); services.AddSingleton(this); - services.AddCoreAbpServices(); + services.AddCoreAbpServices(this); Modules = LoadModules(services, options); } diff --git a/src/Volo.Abp/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs b/src/Volo.Abp/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs index 9127803a0e..cb1029876d 100644 --- a/src/Volo.Abp/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs +++ b/src/Volo.Abp/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs @@ -1,14 +1,21 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Modularity; +using Volo.Abp.Reflection; namespace Volo.Abp.Internal { internal static class InternalServiceCollectionExtensions { - internal static void AddCoreAbpServices(this IServiceCollection services) + internal static void AddCoreAbpServices(this IServiceCollection services, IAbpApplication abpApplication) { - services.TryAddSingleton(new ModuleLoader()); + var moduleLoader = new ModuleLoader(); + var assemblyFinder = new AssemblyFinder(abpApplication); + var typeFinder = new TypeFinder(assemblyFinder); + + services.TryAddSingleton(moduleLoader); + services.TryAddSingleton(assemblyFinder); + services.TryAddSingleton(typeFinder); } } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Reflection/AssemblyFinder.cs b/src/Volo.Abp/Volo/Abp/Reflection/AssemblyFinder.cs index 8178f1046b..0cbe008ab7 100644 --- a/src/Volo.Abp/Volo/Abp/Reflection/AssemblyFinder.cs +++ b/src/Volo.Abp/Volo/Abp/Reflection/AssemblyFinder.cs @@ -4,12 +4,11 @@ using System.Collections.Immutable; using System.Linq; using System.Reflection; using System.Threading; -using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; namespace Volo.Abp.Reflection { - public class AssemblyFinder : IAssemblyFinder, ITransientDependency + public class AssemblyFinder : IAssemblyFinder { private readonly IModuleContainer _moduleContainer; diff --git a/src/Volo.Abp/Volo/Abp/Reflection/TypeFinder.cs b/src/Volo.Abp/Volo/Abp/Reflection/TypeFinder.cs index 619b2f9fd2..6576f08318 100644 --- a/src/Volo.Abp/Volo/Abp/Reflection/TypeFinder.cs +++ b/src/Volo.Abp/Volo/Abp/Reflection/TypeFinder.cs @@ -2,23 +2,18 @@ using System.Collections.Generic; using System.Linq; using System.Threading; -using Microsoft.Extensions.Logging; -using Volo.Abp.DependencyInjection; namespace Volo.Abp.Reflection { - //TODO: What if we need to this type finder while registering dependencies? - public class TypeFinder : ITypeFinder, ITransientDependency + public class TypeFinder : ITypeFinder { private readonly IAssemblyFinder _assemblyFinder; - private readonly ILogger _logger; private readonly Lazy> _types; - public TypeFinder(IAssemblyFinder assemblyFinder, ILogger logger) + public TypeFinder(IAssemblyFinder assemblyFinder) { _assemblyFinder = assemblyFinder; - _logger = logger; _types = new Lazy>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication); } @@ -44,7 +39,7 @@ namespace Volo.Abp.Reflection } catch (Exception ex) { - _logger.LogWarning(ex.ToString()); + //TODO: Trigger a global event? } } diff --git a/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs b/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs index ee3ad9ec57..f6e3939629 100644 --- a/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs +++ b/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs @@ -1,11 +1,9 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.App; -using Volo.Abp.AspNetCore.Modularity; using Volo.Abp.Http.Client; using Volo.Abp.Http.DynamicProxying; using Volo.Abp.Modularity; -using Volo.Abp.TestApp.Application; +using Volo.Abp.TestApp; namespace Volo.Abp.Http { @@ -15,13 +13,10 @@ namespace Volo.Abp.Http public override void ConfigureServices(IServiceCollection services) { services.AddAssemblyOf(); - services.AddHttpClientProxy("/"); - services.AddHttpClientProxy("/"); - services.Configure(options => - { - - }); + services.AddHttpClientProxies(typeof(TestAppModule).Assembly, "/"); + + services.AddHttpClientProxy("/"); } } } diff --git a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs index 4b5a86623d..c87aafa587 100644 --- a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs +++ b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs @@ -2,7 +2,6 @@ using Volo.Abp.Modularity; using Volo.Abp.TestApp.Domain; using Volo.Abp.AutoMapper; -using Volo.Abp.TestApp.Application; using Volo.Abp.TestApp.Application.Dto; namespace Volo.Abp.TestApp diff --git a/test/Volo.Abp.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs b/test/Volo.Abp.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs index c9876875ed..c4be4e9f6d 100644 --- a/test/Volo.Abp.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs +++ b/test/Volo.Abp.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Reflection; -using Microsoft.Extensions.Logging; using NSubstitute; using Shouldly; using Xunit; @@ -24,7 +22,7 @@ namespace Volo.Abp.Reflection //Act - var typeFinder = new TypeFinder(fakeAssemblyFinder, Substitute.For>()); + var typeFinder = new TypeFinder(fakeAssemblyFinder); //Assert