From 1538a21bfd99f4990aebda3ef24ebb8f26e36667 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 16 Sep 2019 15:42:14 +0800 Subject: [PATCH 1/3] Upgrade Autofac. Resolve #1751 --- framework/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj b/framework/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj index 9470f72e19..bc7c696c3d 100644 --- a/framework/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj +++ b/framework/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj @@ -15,7 +15,7 @@ - + From 5622bfc79604b970b5befe3287ef80d91d1c6c8e Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 17 Sep 2019 10:55:43 +0800 Subject: [PATCH 2/3] Prevent the service from being injected again. Resolve #1757 --- ...tCoreMvcConventionalDependencyRegistrar.cs | 32 +++++++---- ...spNetCoreMvcConventionalRegistrar_Tests.cs | 54 +++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs index d49cb6d142..a4f57b1b1f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs @@ -9,22 +9,36 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection { public class AbpAspNetCoreMvcConventionalRegistrar : DefaultConventionalRegistrar { - protected override ServiceLifetime? GetServiceLifetimeFromClassHierarcy(Type type) + public override void AddType(IServiceCollection services, Type type) { - var lifeTime = base.GetServiceLifetimeFromClassHierarcy(type); - if (lifeTime != null) + if (!IsMvcService(type)) { - return lifeTime; + return; } - if (IsController(type) || - IsPageModel(type) || - IsViewComponent(type)) + var lifeTime = GetMvcServiceLifetime(type); + + var serviceTypes = ExposedServiceExplorer.GetExposedServices(type); + + TriggerServiceExposing(services, type, serviceTypes); + + foreach (var serviceType in serviceTypes) { - return ServiceLifetime.Transient; + var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime); + services.Add(serviceDescriptor); } + } - return null; + protected virtual bool IsMvcService(Type type) + { + return IsController(type) || + IsPageModel(type) || + IsViewComponent(type); + } + + protected virtual ServiceLifetime GetMvcServiceLifetime(Type type) + { + return ServiceLifetime.Transient; } private static bool IsPageModel(Type type) diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs new file mode 100644 index 0000000000..46f2823b70 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalRegistrar_Tests.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.DependencyInjection; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection +{ + public class AbpAspNetCoreMvcConventionalRegistrar_Tests + { + [Fact] + public void Should_Registered_Mvc_Service() + { + //Arrange + var services = new ServiceCollection(); + + //Act + services.AddConventionalRegistrar(new DefaultConventionalRegistrar()); + services.AddConventionalRegistrar(new AbpAspNetCoreMvcConventionalRegistrar()); + + services.AddTypes(typeof(My_Test_PageModel), typeof(My_Test_Controller), typeof(My_Test_ViewComponent)); + + //Assert + services.ShouldContainTransient(typeof(My_Test_PageModel)); + services.ShouldContainTransient(typeof(My_Test_Controller)); + services.ShouldContainTransient(typeof(My_Test_ViewComponent)); + + var serviceProvider = services.BuildServiceProvider(); + serviceProvider.GetServices().Count().ShouldBe(1); + serviceProvider.GetServices().Count().ShouldBe(1); + serviceProvider.GetServices().Count().ShouldBe(1); + } + } + + public class My_Test_PageModel : PageModel + { + + } + + public class My_Test_Controller : Controller + { + + } + + public class My_Test_ViewComponent : ViewComponent + { + + } +} From 5959329900dff2569082dcfad24a9901ffa5d763 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 20 Sep 2019 13:22:37 +0800 Subject: [PATCH 3/3] Add application name prefix option to distributed cache item keys. Resolve #1399 --- .../Volo.Abp.Caching/Volo/Abp/Caching/CacheOptions.cs | 6 ++++++ .../Volo/Abp/Caching/DistributedCache.cs | 2 +- .../MyProjectNameHttpApiHostModule.cs | 10 ++++++++++ .../MyProjectNameIdentityServerModule.cs | 6 ++++++ .../MyProjectNameWebModule.cs | 10 ++++++++++ .../MyProjectNameHttpApiHostModule.cs | 6 ++++++ .../MyProjectNameIdentityServerModule.cs | 6 ++++++ .../MyProjectNameWebHostModule.cs | 10 ++++++++++ 8 files changed, 55 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheOptions.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheOptions.cs index 6be122be8f..0400da8c72 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheOptions.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheOptions.cs @@ -6,6 +6,11 @@ namespace Volo.Abp.Caching { public class CacheOptions { + /// + /// Cache key prefix. + /// + public string KeyPrefix { get; set; } + /// /// Global Cache entry options. /// @@ -21,6 +26,7 @@ namespace Volo.Abp.Caching { CacheConfigurators = new List>(); GlobalCacheEntryOptions = new DistributedCacheEntryOptions(); + KeyPrefix = ""; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs index 7b93dc5c23..caa51aeac1 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs @@ -405,7 +405,7 @@ namespace Volo.Abp.Caching protected virtual string NormalizeKey(string key) { - var normalizedKey = "c:" + CacheName + ",k:" + key; + var normalizedKey = "c:" + CacheName + ",k:" + _cacheOption.KeyPrefix + key; if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue) { diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs index feef7b52bd..b341a8428a 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs @@ -15,6 +15,7 @@ using Volo.Abp; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Autofac; +using Volo.Abp.Caching; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.VirtualFileSystem; @@ -41,11 +42,20 @@ namespace MyCompanyName.MyProjectName ConfigureAuthentication(context, configuration); ConfigureSwagger(context); ConfigureLocalization(); + ConfigureCache(configuration); ConfigureVirtualFileSystem(context); ConfigureRedis(context, configuration, hostingEnvironment); ConfigureCors(context, configuration); } + private void ConfigureCache(IConfigurationRoot configuration) + { + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + } + private void ConfigureVirtualFileSystem(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs index a3b3bf27aa..90661e7270 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs @@ -20,6 +20,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.Auditing; using Volo.Abp.Autofac; using Volo.Abp.BackgroundJobs; +using Volo.Abp.Caching; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.UI.Navigation.Urls; @@ -91,6 +92,11 @@ namespace MyCompanyName.MyProjectName options.IsJobExecutionEnabled = false; }); + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + context.Services.AddStackExchangeRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs index 301a53f28d..ae4f874b3c 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebModule.cs @@ -22,6 +22,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; +using Volo.Abp.Caching; using Volo.Abp.FeatureManagement; using Volo.Abp.Http.Client.IdentityModel; using Volo.Abp.Identity.Web; @@ -68,6 +69,7 @@ namespace MyCompanyName.MyProjectName.Web var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + ConfigureCache(configuration); ConfigureUrls(configuration); ConfigureAuthentication(context, configuration); ConfigureAutoMapper(); @@ -77,6 +79,14 @@ namespace MyCompanyName.MyProjectName.Web ConfigureMultiTenancy(); } + private void ConfigureCache(IConfigurationRoot configuration) + { + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + } + private void ConfigureUrls(IConfigurationRoot configuration) { Configure(options => diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs index e0a26599d2..9637480296 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs @@ -11,6 +11,7 @@ using Volo.Abp; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; +using Volo.Abp.Caching; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.SqlServer; using Volo.Abp.Localization; @@ -86,6 +87,11 @@ namespace MyCompanyName.MyProjectName options.ApiName = "MyProjectName"; }); + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + context.Services.AddStackExchangeRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs index bde2d913bd..50fe7bccef 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs @@ -14,6 +14,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.Auditing; using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; +using Volo.Abp.Caching; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.SqlServer; @@ -105,6 +106,11 @@ namespace MyCompanyName.MyProjectName options.ApiName = configuration["AuthServer:ApiName"]; }); + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + context.Services.AddStackExchangeRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs index 71d5f6507f..885f072d1f 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs @@ -22,6 +22,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; +using Volo.Abp.Caching; using Volo.Abp.Http.Client.IdentityModel; using Volo.Abp.Identity; using Volo.Abp.Identity.Web; @@ -72,6 +73,7 @@ namespace MyCompanyName.MyProjectName var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + ConfigureCache(configuration); ConfigureUrls(configuration); ConfigureAuthentication(context, configuration); ConfigureAutoMapper(); @@ -80,6 +82,14 @@ namespace MyCompanyName.MyProjectName ConfigureMultiTenancy(); ConfigureRedis(context, configuration, hostingEnvironment); } + + private void ConfigureCache(IConfigurationRoot configuration) + { + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + } private void ConfigureUrls(IConfigurationRoot configuration) {