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/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/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 + { + + } +} 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 09c3779dcc..d2ad957c64 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 @@ -16,6 +16,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,6 +42,7 @@ namespace MyCompanyName.MyProjectName ConfigureConventionalControllers(); ConfigureAuthentication(context, configuration); ConfigureLocalization(); + ConfigureCache(configuration); ConfigureVirtualFileSystem(context); ConfigureRedis(context, configuration, hostingEnvironment); ConfigureCors(context, configuration); @@ -49,6 +51,14 @@ namespace MyCompanyName.MyProjectName //ConfigureSwaggerServices(context); } + 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 7fdf49ee3b..9afb2e5263 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 4e7bd48d4d..9eda18be4d 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 @@ -23,6 +23,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; @@ -69,6 +70,7 @@ namespace MyCompanyName.MyProjectName.Web 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.Web //ConfigureSwaggerServices(context.Services); } + 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 b2b8dc8f70..2d0190b60d 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 fd0d299974..397afc38c3 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs @@ -15,6 +15,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; @@ -106,6 +107,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 681e64d857..5c1318e32b 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 @@ -23,6 +23,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; @@ -73,6 +74,7 @@ namespace MyCompanyName.MyProjectName var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + ConfigureCache(configuration); ConfigureUrls(configuration); ConfigureAuthentication(context, configuration); ConfigureAutoMapper(); @@ -81,6 +83,14 @@ namespace MyCompanyName.MyProjectName ConfigureMultiTenancy(); ConfigureRedis(context, configuration, hostingEnvironment); } + + private void ConfigureCache(IConfigurationRoot configuration) + { + Configure(options => + { + options.KeyPrefix = "MyProjectName:"; + }); + } private void ConfigureUrls(IConfigurationRoot configuration) {