From c62af908a4d7bb373a84bf8766fe858b7a1ec160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 11 Aug 2022 13:47:03 +0300 Subject: [PATCH] Introduce IAbpApplication.ApplicationName --- .../ServiceCollectionApplicationExtensions.cs | 6 ++++++ .../Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs | 4 ++++ .../Volo/Abp/AbpApplicationCreationOptions.cs | 3 +++ .../AbpApplicationWithInternalServiceProvider.cs | 2 +- .../src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs | 7 +++++++ .../Volo/Abp/AbpApplication_Initialize_Tests.cs | 15 +++++++++++++++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionApplicationExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionApplicationExtensions.cs index ce4af2befa..204d582ca5 100644 --- a/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionApplicationExtensions.cs +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionApplicationExtensions.cs @@ -39,4 +39,10 @@ public static class ServiceCollectionApplicationExtensions { return await AbpApplicationFactory.CreateAsync(startupModuleType, services, optionsAction); } + + [CanBeNull] + public static string GetApplicationName(this IServiceCollection services) + { + return services.GetSingletonInstance().ApplicationName; + } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index 741f6f0586..e24403d0e3 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -23,6 +23,8 @@ public abstract class AbpApplicationBase : IAbpApplication public IServiceCollection Services { get; } public IReadOnlyList Modules { get; } + + public string ApplicationName { get; } private bool _configuredServices; @@ -41,6 +43,8 @@ public abstract class AbpApplicationBase : IAbpApplication var options = new AbpApplicationCreationOptions(services); optionsAction?.Invoke(options); + + ApplicationName = options.ApplicationName; services.AddSingleton(this); services.AddSingleton(this); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs index d18e43275b..24a1bf8ee6 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs @@ -20,6 +20,9 @@ public class AbpApplicationCreationOptions public AbpConfigurationBuilderOptions Configuration { get; } public bool SkipConfigureServices { get; set; } + + [CanBeNull] + public string ApplicationName { get; set; } public AbpApplicationCreationOptions([NotNull] IServiceCollection services) { diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs index 245ce6fb06..5f09157fcc 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs @@ -60,6 +60,6 @@ internal class AbpApplicationWithInternalServiceProvider : AbpApplicationBase, I public override void Dispose() { base.Dispose(); - ServiceScope.Dispose(); + ServiceScope?.Dispose(); } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs index 2fb66b735f..e132df5a59 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs @@ -24,6 +24,13 @@ public interface IAbpApplication : IModuleContainer, IDisposable /// IServiceProvider ServiceProvider { get; } + /// + /// Name of the application. + /// This is useful for systems with multiple applications, to distinguish + /// resources of the applications located together. + /// + string ApplicationName { get; } + /// /// Calls the Pre/Post/ConfigureServicesAsync methods of the modules. /// If you use this method, you must have set the diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs index f69d94ce61..4ce2bf1d36 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs @@ -142,4 +142,19 @@ public class AbpApplication_Initialize_Tests plugInModule.OnApplicationShutdownIsCalled.ShouldBeTrue(); } } + + [Fact] + public void Should_Set_And_Get_ApplicationName() + { + const string applicationName = "MyApplication"; + + using (var application = AbpApplicationFactory.Create(options => + { + options.ApplicationName = applicationName; + })) + { + application.ApplicationName.ShouldBe(applicationName); + application.Services.GetApplicationName().ShouldBe(applicationName); + } + } }