diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/CachedApplicationConfigurationClient.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/CachedApplicationConfigurationClient.cs index 4ceb8bdd1c..838150fd49 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/CachedApplicationConfigurationClient.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/CachedApplicationConfigurationClient.cs @@ -13,8 +13,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Client { public class CachedApplicationConfigurationClient : ICachedApplicationConfigurationClient, ITransientDependency { - public IHttpContextAccessor HttpContextAccessor { get; set; } - + protected IHttpContextAccessor HttpContextAccessor { get; } protected IHttpClientProxy Proxy { get; } protected ICurrentUser CurrentUser { get; } protected IDistributedCache Cache { get; } @@ -42,11 +41,11 @@ namespace Volo.Abp.AspNetCore.Mvc.Client } configuration = await Cache.GetOrAddAsync( - CreateCacheKey(), + cacheKey, async () => await Proxy.Service.GetAsync(), () => new DistributedCacheEntryOptions { - AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60) //TODO: Should be configurable. Default value should be higher (5 mins would be good). + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120) //TODO: Should be configurable. Default value should be higher (5 mins would be good). } ); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/RemoteTenantStore.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/RemoteTenantStore.cs new file mode 100644 index 0000000000..718abbb6a9 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/RemoteTenantStore.cs @@ -0,0 +1,116 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Caching.Distributed; +using Volo.Abp.AspNetCore.Mvc.MultiTenancy; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Client.DynamicProxying; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Threading; + +namespace Volo.Abp.AspNetCore.Mvc.Client +{ + public class RemoteTenantStore : ITenantStore, ITransientDependency + { + protected IHttpClientProxy Proxy { get; } + protected IHttpContextAccessor HttpContextAccessor { get; } + protected IDistributedCache Cache { get; } + + public RemoteTenantStore( + IHttpClientProxy proxy, + IHttpContextAccessor httpContextAccessor, + IDistributedCache cache) + { + Proxy = proxy; + HttpContextAccessor = httpContextAccessor; + Cache = cache; + } + + public async Task FindAsync(string name) + { + var cacheKey = CreateCacheKey(name); + var httpContext = HttpContextAccessor?.HttpContext; + + if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) + { + return tenantConfiguration; + } + + tenantConfiguration = await Cache.GetOrAddAsync( + cacheKey, + async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByNameAsync(name)), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + if (httpContext != null) + { + httpContext.Items[cacheKey] = tenantConfiguration; + } + + return tenantConfiguration; + } + + public async Task FindAsync(Guid id) + { + var cacheKey = CreateCacheKey(id); + var httpContext = HttpContextAccessor?.HttpContext; + + if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) + { + return tenantConfiguration; + } + + tenantConfiguration = await Cache.GetOrAddAsync( + cacheKey, + async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByIdAsync(id)), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + if (httpContext != null) + { + httpContext.Items[cacheKey] = tenantConfiguration; + } + + return tenantConfiguration; + } + + public TenantConfiguration Find(string name) + { + return AsyncHelper.RunSync(() => FindAsync(name)); + } + + public TenantConfiguration Find(Guid id) + { + return AsyncHelper.RunSync(() => FindAsync(id)); + } + + protected virtual TenantConfiguration CreateTenantConfiguration(FindTenantResultDto tenantResultDto) + { + if (!tenantResultDto.Success || tenantResultDto.TenantId == null) + { + return null; + } + + return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name); + } + + protected virtual string CreateCacheKey(string tenantName) + { + return $"RemoteTenantStore_Name_{tenantName}"; + } + + protected virtual string CreateCacheKey(Guid tenantId) + { + return $"RemoteTenantStore_Id_{tenantId:N}"; + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResult.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResultDto.cs similarity index 78% rename from framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResult.cs rename to framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResultDto.cs index 09688f5a6a..d85622f0cf 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResult.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/FindTenantResultDto.cs @@ -2,7 +2,8 @@ namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy { - public class FindTenantResult + [Serializable] + public class FindTenantResultDto { public bool Success { get; set; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/IAbpTenantAppService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/IAbpTenantAppService.cs index eb48f9ded0..eb02e84ceb 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/IAbpTenantAppService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/IAbpTenantAppService.cs @@ -6,8 +6,8 @@ namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy { public interface IAbpTenantAppService : IApplicationService { - Task FindTenantByNameAsync(string name); + Task FindTenantByNameAsync(string name); - Task FindTenantByIdAsync(Guid id); + Task FindTenantByIdAsync(Guid id); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantAppService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantAppService.cs index 394ac1ced3..d0bea87196 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantAppService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantAppService.cs @@ -15,16 +15,16 @@ namespace Pages.Abp.MultiTenancy TenantStore = tenantStore; } - public async Task FindTenantByNameAsync(string name) + public async Task FindTenantByNameAsync(string name) { var tenant = await TenantStore.FindAsync(name); if (tenant == null) { - return new FindTenantResult { Success = false }; + return new FindTenantResultDto { Success = false }; } - return new FindTenantResult + return new FindTenantResultDto { Success = true, TenantId = tenant.Id, @@ -32,16 +32,16 @@ namespace Pages.Abp.MultiTenancy }; } - public async Task FindTenantByIdAsync(Guid id) + public async Task FindTenantByIdAsync(Guid id) { var tenant = await TenantStore.FindAsync(id); if (tenant == null) { - return new FindTenantResult { Success = false }; + return new FindTenantResultDto { Success = false }; } - return new FindTenantResult + return new FindTenantResultDto { Success = true, TenantId = tenant.Id, diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantController.cs index 2051de5651..5da47875d9 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy/Pages/Abp/MultiTenancy/AbpTenantController.cs @@ -19,14 +19,14 @@ namespace Pages.Abp.MultiTenancy [HttpGet] [Route("find-tenant/{name}")] //TODO: Remove on v1.0 [Route("tenants/by-name/{name}")] - public async Task FindTenantByNameAsync(string name) + public async Task FindTenantByNameAsync(string name) { return await _abpTenantAppService.FindTenantByNameAsync(name); } [HttpGet] [Route("tenants/by-id/{id}")] - public async Task FindTenantByIdAsync(Guid id) + public async Task FindTenantByIdAsync(Guid id) { return await _abpTenantAppService.FindTenantByIdAsync(id); }