mirror of https://github.com/abpframework/abp.git
17 changed files with 180 additions and 7 deletions
@ -0,0 +1,22 @@ |
|||||
|
using System.Net.Http; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Http.DynamicProxying; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.TestBase.DynamicProxying |
||||
|
{ |
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class AspNetCoreTestDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency |
||||
|
{ |
||||
|
private readonly ITestServerAccessor _testServerAccessor; |
||||
|
|
||||
|
public AspNetCoreTestDynamicProxyHttpClientFactory(ITestServerAccessor testServerAccessor) |
||||
|
{ |
||||
|
_testServerAccessor = testServerAccessor; |
||||
|
} |
||||
|
|
||||
|
public HttpClient Create() |
||||
|
{ |
||||
|
return _testServerAccessor.Server.CreateClient(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Microsoft.AspNetCore.TestHost; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.TestBase |
||||
|
{ |
||||
|
public interface ITestServerAccessor |
||||
|
{ |
||||
|
TestServer Server { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Microsoft.AspNetCore.TestHost; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.TestBase |
||||
|
{ |
||||
|
public class TestServerAccessor : ITestServerAccessor, ISingletonDependency |
||||
|
{ |
||||
|
public TestServer Server { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using System; |
||||
|
using Castle.DynamicProxy; |
||||
|
using Volo.Abp.Castle.DynamicProxy; |
||||
|
using Volo.Abp.Http; |
||||
|
using Volo.Abp.Http.DynamicProxying; |
||||
|
|
||||
|
namespace Microsoft.Extensions.DependencyInjection |
||||
|
{ |
||||
|
public static class ServiceCollectionHttpProxyExtensions |
||||
|
{ |
||||
|
private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator(); |
||||
|
|
||||
|
public static IServiceCollection AddHttpClientProxy<T>(this IServiceCollection services, string baseUrl) |
||||
|
{ |
||||
|
return services.AddHttpClientProxy(typeof(T), baseUrl); |
||||
|
} |
||||
|
|
||||
|
public static IServiceCollection AddHttpClientProxy(this IServiceCollection services, Type type, string baseUrl) |
||||
|
{ |
||||
|
services.AddTransient(type, serviceProvider => |
||||
|
{ |
||||
|
return ProxyGeneratorInstance |
||||
|
.CreateInterfaceProxyWithoutTarget( |
||||
|
type, |
||||
|
serviceProvider.GetRequiredService<CastleAbpInterceptorAdapter<DynamicHttpProxyInterceptor>>() |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
return services; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System.Net.Http; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Http.DynamicProxying |
||||
|
{ |
||||
|
public class DefaultDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency |
||||
|
{ |
||||
|
public HttpClient Create() |
||||
|
{ |
||||
|
return new HttpClient(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Net.Http; |
||||
|
using System.Reflection; |
||||
|
using System.Threading.Tasks; |
||||
|
using Newtonsoft.Json; |
||||
|
using Newtonsoft.Json.Serialization; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.DynamicProxy; |
||||
|
|
||||
|
namespace Volo.Abp.Http.DynamicProxying |
||||
|
{ |
||||
|
public class DynamicHttpProxyInterceptor : AbpInterceptor, ITransientDependency |
||||
|
{ |
||||
|
private readonly IDynamicProxyHttpClientFactory _httpClientFactory; |
||||
|
|
||||
|
public DynamicHttpProxyInterceptor(IDynamicProxyHttpClientFactory httpClientFactory) |
||||
|
{ |
||||
|
_httpClientFactory = httpClientFactory; |
||||
|
} |
||||
|
|
||||
|
public override void Intercept(IAbpMethodInvocation invocation) |
||||
|
{ |
||||
|
throw new System.NotImplementedException(); |
||||
|
} |
||||
|
|
||||
|
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
||||
|
{ |
||||
|
var returnTypeWithoutTask = invocation.Method.ReturnType.GenericTypeArguments[0]; |
||||
|
|
||||
|
//var result = await GetResult(client, returnTypeWithoutTask);
|
||||
|
|
||||
|
var methods = typeof(DynamicHttpProxyInterceptor).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); |
||||
|
var getResultMethod = methods |
||||
|
.Where(m => m.Name == nameof(GetResult)) |
||||
|
.First() |
||||
|
.MakeGenericMethod(returnTypeWithoutTask); |
||||
|
|
||||
|
invocation.ReturnValue = getResultMethod.Invoke(this, new object[] { returnTypeWithoutTask }); |
||||
|
} |
||||
|
|
||||
|
private async Task<T> GetResult<T>(Type returnTypeWithoutTask) |
||||
|
{ |
||||
|
using (var client = _httpClientFactory.Create()) |
||||
|
{ |
||||
|
var response = await client.GetAsync("/api/app/people"); |
||||
|
if (!response.IsSuccessStatusCode) |
||||
|
{ |
||||
|
throw new AbpException("Remote service returns error!"); |
||||
|
} |
||||
|
|
||||
|
var content = await response.Content.ReadAsStringAsync(); |
||||
|
|
||||
|
var result = JsonConvert.DeserializeObject( |
||||
|
content, |
||||
|
returnTypeWithoutTask, |
||||
|
new JsonSerializerSettings |
||||
|
{ |
||||
|
ContractResolver = new CamelCasePropertyNamesContractResolver() |
||||
|
}); |
||||
|
|
||||
|
return (T)result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Net.Http; |
||||
|
|
||||
|
namespace Volo.Abp.Http.DynamicProxying |
||||
|
{ |
||||
|
public interface IDynamicProxyHttpClientFactory |
||||
|
{ |
||||
|
HttpClient Create(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue