Browse Source

Introduced RemoteServiceOptions.

pull/113/head
Halil İbrahim Kalkan 9 years ago
parent
commit
b2c0fad53a
  1. 4
      src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs
  2. 6
      src/AbpDesk/AbpDesk.Web.Mvc/appsettings.json
  3. 14
      src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs
  4. 7
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
  5. 8
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpClientProxyConfig.cs
  6. 19
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
  7. 5
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IApiDescriptionFinder.cs
  8. 42
      src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/RemoteServiceOptions.cs
  9. 1
      src/Volo.Abp.Identity.HttpApi.Client/Volo.Abp.Identity.HttpApi.Client.csproj
  10. 5
      src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/AbpIdentityHttpApiClientModule.cs
  11. 7
      src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/AbpIdentityHttpApiClientOptions.cs
  12. 62
      src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/HttpApiUserAppService.cs
  13. 6
      src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/AbpIdentityHttpApiModule.cs
  14. 33
      src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUsersController.cs
  15. 8
      test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs

4
src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs

@ -19,6 +19,7 @@ using Volo.Abp.Identity.Web;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Timing; using Volo.Abp.Timing;
using Volo.Abp.Ui.Navigation; using Volo.Abp.Ui.Navigation;
using Volo.Abp.Http.Client;
namespace AbpDesk.Web.Mvc namespace AbpDesk.Web.Mvc
{ {
@ -45,7 +46,8 @@ namespace AbpDesk.Web.Mvc
options.MenuContributors.Add(new MainMenuContributor()); options.MenuContributors.Add(new MainMenuContributor());
}); });
services.Configure<AbpIdentityHttpApiClientOptions>(configuration.GetSection("AbpIdentity:HttpApiClient")); //TODO: Remove Http.Client reference later
services.Configure<RemoteServiceOptions>(configuration);
services.AddMvc(); services.AddMvc();

6
src/AbpDesk/AbpDesk.Web.Mvc/appsettings.json

@ -3,9 +3,9 @@
"Default": "Server=localhost;Database=AbpDesk;Trusted_Connection=True;", "Default": "Server=localhost;Database=AbpDesk;Trusted_Connection=True;",
"AbpDeskMongoBlog": "mongodb://127.0.0.1:27017|AbpDeskBlog" "AbpDeskMongoBlog": "mongodb://127.0.0.1:27017|AbpDeskBlog"
}, },
"AbpIdentity": { "RemoteServices": {
"HttpApiClient": { "AbpIdentity": {
"ApiUrlBase": "http://localhost:63290/" "BaseUrl": "http://localhost:63290/"
} }
} }
} }

14
src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs

@ -6,7 +6,6 @@ using Volo.Abp.Application.Services;
using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Http.Client; using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.DynamicProxying; using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.Http.Modeling;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
@ -17,8 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddHttpClientProxies( public static IServiceCollection AddHttpClientProxies(
this IServiceCollection services, this IServiceCollection services,
Assembly assembly, Assembly assembly,
string baseUrl, string remoteServiceName)
string moduleName = ModuleApiDescriptionModel.DefaultModuleName)
{ {
//TODO: Add option to change type filter //TODO: Add option to change type filter
@ -28,22 +26,22 @@ namespace Microsoft.Extensions.DependencyInjection
foreach (var serviceType in serviceTypes) foreach (var serviceType in serviceTypes)
{ {
services.AddHttpClientProxy(serviceType, baseUrl); services.AddHttpClientProxy(serviceType, remoteServiceName);
} }
return services; return services;
} }
public static IServiceCollection AddHttpClientProxy<T>(this IServiceCollection services, string baseUrl) public static IServiceCollection AddHttpClientProxy<T>(this IServiceCollection services, string remoteServiceName)
{ {
return services.AddHttpClientProxy(typeof(T), baseUrl); return services.AddHttpClientProxy(typeof(T), remoteServiceName);
} }
public static IServiceCollection AddHttpClientProxy(this IServiceCollection services, Type type, string baseUrl) public static IServiceCollection AddHttpClientProxy(this IServiceCollection services, Type type, string remoteServiceName)
{ {
services.Configure<AbpHttpClientOptions>(options => services.Configure<AbpHttpClientOptions>(options =>
{ {
options.HttpClientProxies[type] = new DynamicHttpClientProxyConfig(baseUrl, type); options.HttpClientProxies[type] = new DynamicHttpClientProxyConfig(type, remoteServiceName);
}); });
var interceptorType = typeof(DynamicHttpProxyInterceptor<>).MakeGenericType(type); var interceptorType = typeof(DynamicHttpProxyInterceptor<>).MakeGenericType(type);

7
src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs

@ -1,4 +1,5 @@
using System.Linq; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -15,7 +16,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
_descriptionCache = descriptionCache; _descriptionCache = descriptionCache;
} }
public async Task<ActionApiDescriptionModel> FindActionAsync(DynamicHttpClientProxyConfig proxyConfig, MethodInfo method) public async Task<ActionApiDescriptionModel> FindActionAsync(RemoteServiceConfiguration proxyConfig, Type serviceType, MethodInfo method)
{ {
var apiDescription = await _descriptionCache.GetAsync(proxyConfig.BaseUrl); var apiDescription = await _descriptionCache.GetAsync(proxyConfig.BaseUrl);
@ -27,7 +28,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
foreach (var controller in module.Controllers.Values) foreach (var controller in module.Controllers.Values)
{ {
if (!controller.Implements(proxyConfig.Type)) if (!controller.Implements(serviceType))
{ {
continue; continue;
} }

8
src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpClientProxyConfig.cs

@ -4,14 +4,14 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
public class DynamicHttpClientProxyConfig public class DynamicHttpClientProxyConfig
{ {
public string BaseUrl { get; }
public Type Type { get; } public Type Type { get; }
public DynamicHttpClientProxyConfig(string baseUrl, Type type) public string RemoteServiceName { get; }
public DynamicHttpClientProxyConfig(Type type, string remoteServiceName)
{ {
BaseUrl = baseUrl;
Type = type; Type = type;
RemoteServiceName = remoteServiceName;
} }
} }
} }

19
src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs

@ -21,7 +21,8 @@ namespace Volo.Abp.Http.Client.DynamicProxying
private readonly IDynamicProxyHttpClientFactory _httpClientFactory; private readonly IDynamicProxyHttpClientFactory _httpClientFactory;
private readonly IApiDescriptionFinder _apiDescriptionFinder; private readonly IApiDescriptionFinder _apiDescriptionFinder;
private readonly AbpHttpClientOptions _options; private readonly RemoteServiceOptions _remoteServiceOptions;
private readonly AbpHttpClientOptions _clientOptions;
private readonly IJsonSerializer _jsonSerializer; private readonly IJsonSerializer _jsonSerializer;
static DynamicHttpProxyInterceptor() static DynamicHttpProxyInterceptor()
@ -33,14 +34,16 @@ namespace Volo.Abp.Http.Client.DynamicProxying
public DynamicHttpProxyInterceptor( public DynamicHttpProxyInterceptor(
IDynamicProxyHttpClientFactory httpClientFactory, IDynamicProxyHttpClientFactory httpClientFactory,
IOptions<AbpHttpClientOptions> options, IOptions<AbpHttpClientOptions> clientOptions,
IOptionsSnapshot<RemoteServiceOptions> remoteServiceOptions,
IApiDescriptionFinder apiDescriptionFinder, IApiDescriptionFinder apiDescriptionFinder,
IJsonSerializer jsonSerializer) IJsonSerializer jsonSerializer)
{ {
_httpClientFactory = httpClientFactory; _httpClientFactory = httpClientFactory;
_apiDescriptionFinder = apiDescriptionFinder; _apiDescriptionFinder = apiDescriptionFinder;
_jsonSerializer = jsonSerializer; _jsonSerializer = jsonSerializer;
_options = options.Value; _clientOptions = clientOptions.Value;
_remoteServiceOptions = remoteServiceOptions.Value;
} }
public override void Intercept(IAbpMethodInvocation invocation) public override void Intercept(IAbpMethodInvocation invocation)
@ -82,7 +85,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
using (var client = _httpClientFactory.Create()) using (var client = _httpClientFactory.Create())
{ {
var proxyConfig = GetProxyConfig(); var proxyConfig = GetProxyConfig();
var action = await _apiDescriptionFinder.FindActionAsync(proxyConfig, invocation.Method); var action = await _apiDescriptionFinder.FindActionAsync(proxyConfig, typeof(TService), invocation.Method);
var url = proxyConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary); var url = proxyConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary);
var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url) var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url)
@ -115,10 +118,14 @@ namespace Volo.Abp.Http.Client.DynamicProxying
} }
} }
private DynamicHttpClientProxyConfig GetProxyConfig() private RemoteServiceConfiguration GetProxyConfig()
{ {
return _options.HttpClientProxies.GetOrDefault(typeof(TService)) var clientConfig = _clientOptions.HttpClientProxies.GetOrDefault(typeof(TService))
?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}."); ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
return _remoteServiceOptions.RemoteServices.GetOrDefault(clientConfig.RemoteServiceName)
?? _remoteServiceOptions.RemoteServices.Default
?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
} }
} }
} }

5
src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IApiDescriptionFinder.cs

@ -1,4 +1,5 @@
using System.Reflection; using System;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Http.Modeling; using Volo.Abp.Http.Modeling;
@ -6,6 +7,6 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
public interface IApiDescriptionFinder public interface IApiDescriptionFinder
{ {
Task<ActionApiDescriptionModel> FindActionAsync(DynamicHttpClientProxyConfig proxyConfig, MethodInfo invocationMethod); Task<ActionApiDescriptionModel> FindActionAsync(RemoteServiceConfiguration proxyConfig, Type serviceType, MethodInfo invocationMethod);
} }
} }

42
src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/RemoteServiceOptions.cs

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Volo.Abp.Http.Client
{
public class RemoteServiceOptions
{
public RemoteServiceDictionary RemoteServices { get; set; }
public RemoteServiceOptions()
{
RemoteServices = new RemoteServiceDictionary();
}
}
public class RemoteServiceDictionary : Dictionary<string, RemoteServiceConfiguration>
{
public const string DefaultName = "Default";
public RemoteServiceConfiguration Default
{
get { return this.GetOrDefault(DefaultName); }
set { this[DefaultName] = value; }
}
}
public class RemoteServiceConfiguration
{
public string BaseUrl { get; set; }
public RemoteServiceConfiguration()
{
}
public RemoteServiceConfiguration(string baseUrl)
{
BaseUrl = baseUrl;
}
}
}

1
src/Volo.Abp.Identity.HttpApi.Client/Volo.Abp.Identity.HttpApi.Client.csproj

@ -12,6 +12,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" />
<ProjectReference Include="..\Volo.Abp.Identity.Application.Contracts\Volo.Abp.Identity.Application.Contracts.csproj" /> <ProjectReference Include="..\Volo.Abp.Identity.Application.Contracts\Volo.Abp.Identity.Application.Contracts.csproj" />
</ItemGroup> </ItemGroup>

5
src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/AbpIdentityHttpApiClientModule.cs

@ -1,14 +1,17 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
[DependsOn(typeof(AbpIdentityApplicationContractsModule))] [DependsOn(typeof(AbpIdentityApplicationContractsModule), typeof(AbpHttpClientModule))]
public class AbpIdentityHttpApiClientModule : AbpModule public class AbpIdentityHttpApiClientModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
services.AddAssemblyOf<AbpIdentityHttpApiClientModule>(); services.AddAssemblyOf<AbpIdentityHttpApiClientModule>();
services.AddHttpClientProxy<AbpIdentityApplicationContractsModule>("AbpIdentity");
} }
} }
} }

7
src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/AbpIdentityHttpApiClientOptions.cs

@ -1,7 +0,0 @@
namespace Volo.Abp.Identity
{
public class AbpIdentityHttpApiClientOptions
{
public string ApiUrlBase { get; set; }
}
}

62
src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/HttpApiUserAppService.cs

@ -1,62 +0,0 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.Identity
{
public class HttpApiUserAppService : IUserAppService
{
private readonly AbpIdentityHttpApiClientOptions _options;
public HttpApiUserAppService(IOptionsSnapshot<AbpIdentityHttpApiClientOptions> options)
{
_options = options.Value;
}
public async Task<ListResultDto<IdentityUserDto>> Get()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(_options.ApiUrlBase + "api/identity/users");
if (!response.IsSuccessStatusCode)
{
throw new AbpException("Remote service returns error!");
}
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ListResultDto<IdentityUserDto>>(
content,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
}
}
public async Task<IdentityUserDto> Get(Guid id)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(_options.ApiUrlBase + "api/identity/users/" + id);
if (!response.IsSuccessStatusCode)
{
throw new AbpException("Remote service returns error!");
}
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IdentityUserDto>(
content,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
}
}
}
}

6
src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/AbpIdentityHttpApiModule.cs

@ -10,6 +10,12 @@ namespace Volo.Abp.Identity
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
services.AddAssemblyOf<AbpIdentityHttpApiModule>(); services.AddAssemblyOf<AbpIdentityHttpApiModule>();
services.Configure<AbpAspNetCoreMvcOptions>(options =>
{
//TODO: We can move this call to services.AddAppServicesAsControllers(typeof(AbpIdentityApplicationContractsModule).Assembly, "identity");
options.AppServiceControllers.CreateFor(typeof(AbpIdentityApplicationModule).Assembly, "identity");
});
} }
} }
} }

33
src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUsersController.cs

@ -1,33 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
namespace Volo.Abp.Identity
{
[Route("api/identity/users")]
public class IdentityUsersController : AbpController
{
private readonly IUserAppService _userAppService;
public IdentityUsersController(IUserAppService userAppService)
{
_userAppService = userAppService;
}
[HttpGet]
[Route("")]
public Task<ListResultDto<IdentityUserDto>> Get()
{
return _userAppService.Get();
}
[HttpGet]
[Route("{id}")]
public Task<IdentityUserDto> Get(Guid id)
{
return _userAppService.Get(id);
}
}
}

8
test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpTestModule.cs

@ -14,9 +14,13 @@ namespace Volo.Abp.Http
{ {
services.AddAssemblyOf<AbpHttpTestModule>(); services.AddAssemblyOf<AbpHttpTestModule>();
services.AddHttpClientProxies(typeof(TestAppModule).Assembly, "/"); services.AddHttpClientProxies(typeof(TestAppModule).Assembly, "Default");
services.AddHttpClientProxy<IRegularTestController>("Default");
services.AddHttpClientProxy<IRegularTestController>("/"); services.Configure<RemoteServiceOptions>(options =>
{
options.RemoteServices.Default = new RemoteServiceConfiguration("/");
});
} }
} }
} }

Loading…
Cancel
Save