Browse Source

Created ServiceCollectionDynamicHttpClientProxyExtensions.AddHttpClientProxies.

pull/113/head
Halil İbrahim Kalkan 9 years ago
parent
commit
d9cf5c7d22
  1. 25
      src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs
  2. 2
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
  3. 5
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
  4. 2
      src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs
  5. 11
      src/Volo.Abp/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs
  6. 3
      src/Volo.Abp/Volo/Abp/Reflection/AssemblyFinder.cs
  7. 11
      src/Volo.Abp/Volo/Abp/Reflection/TypeFinder.cs
  8. 15
      test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs
  9. 1
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs
  10. 6
      test/Volo.Abp.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs

25
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<T>(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)
)
);
}

2
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)

5
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;

2
src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs

@ -34,7 +34,7 @@ namespace Volo.Abp
services.AddSingleton<IAbpApplication>(this);
services.AddSingleton<IModuleContainer>(this);
services.AddCoreAbpServices();
services.AddCoreAbpServices(this);
Modules = LoadModules(services, options);
}

11
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<IModuleLoader>(new ModuleLoader());
var moduleLoader = new ModuleLoader();
var assemblyFinder = new AssemblyFinder(abpApplication);
var typeFinder = new TypeFinder(assemblyFinder);
services.TryAddSingleton<IModuleLoader>(moduleLoader);
services.TryAddSingleton<IAssemblyFinder>(assemblyFinder);
services.TryAddSingleton<ITypeFinder>(typeFinder);
}
}
}

3
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;

11
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<TypeFinder> _logger;
private readonly Lazy<IReadOnlyList<Type>> _types;
public TypeFinder(IAssemblyFinder assemblyFinder, ILogger<TypeFinder> logger)
public TypeFinder(IAssemblyFinder assemblyFinder)
{
_assemblyFinder = assemblyFinder;
_logger = logger;
_types = new Lazy<IReadOnlyList<Type>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication);
}
@ -44,7 +39,7 @@ namespace Volo.Abp.Reflection
}
catch (Exception ex)
{
_logger.LogWarning(ex.ToString());
//TODO: Trigger a global event?
}
}

15
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<AbpHttpTestModule>();
services.AddHttpClientProxy<IPeopleAppService>("/");
services.AddHttpClientProxy<IRegularTestController>("/");
services.Configure<MvcOptions>(options =>
{
});
services.AddHttpClientProxies(typeof(TestAppModule).Assembly, "/");
services.AddHttpClientProxy<IRegularTestController>("/");
}
}
}

1
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

6
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<ILogger<TypeFinder>>());
var typeFinder = new TypeFinder(fakeAssemblyFinder);
//Assert

Loading…
Cancel
Save