Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get application name's default value from the entrance assembly #14403

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Extensions.Configuration;
using JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Volo.Abp;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -11,14 +13,22 @@ public static IServiceCollection ReplaceConfiguration(this IServiceCollection se
return services.Replace(ServiceDescriptor.Singleton<IConfiguration>(configuration));
}

[NotNull]
public static IConfiguration GetConfiguration(this IServiceCollection services)
{
return services.GetConfigurationOrNull() ??
throw new AbpException("Could not find an implementation of " + typeof(IConfiguration).AssemblyQualifiedName + " in the service collection.");
}

[CanBeNull]
public static IConfiguration GetConfigurationOrNull(this IServiceCollection services)
{
var hostBuilderContext = services.GetSingletonInstanceOrNull<HostBuilderContext>();
if (hostBuilderContext?.Configuration != null)
{
return hostBuilderContext.Configuration as IConfigurationRoot;
}

return services.GetSingletonInstance<IConfiguration>();
return services.GetSingletonInstanceOrNull<IConfiguration>();
}
}
34 changes: 30 additions & 4 deletions framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class AbpApplicationBase : IAbpApplication
public IServiceCollection Services { get; }

public IReadOnlyList<IAbpModuleDescriptor> Modules { get; }

public string ApplicationName { get; }

private bool _configuredServices;
Expand All @@ -43,8 +43,8 @@ internal AbpApplicationBase(

var options = new AbpApplicationCreationOptions(services);
optionsAction?.Invoke(options);
ApplicationName = options.ApplicationName;

ApplicationName = GetApplicationName(options);

services.AddSingleton<IAbpApplication>(this);
services.AddSingleton<IApplicationNameAccessor>(this);
Expand Down Expand Up @@ -147,7 +147,7 @@ protected virtual IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollec
public virtual async Task ConfigureServicesAsync()
{
CheckMultipleConfigureServices();

var context = new ServiceConfigurationContext(Services);
Services.AddSingleton(context);

Expand Down Expand Up @@ -312,4 +312,30 @@ public virtual void ConfigureServices()

_configuredServices = true;
}

private static string GetApplicationName(AbpApplicationCreationOptions options)
{
if (!string.IsNullOrWhiteSpace(options.ApplicationName))
{
return options.ApplicationName;
}

var configuration = options.Services.GetConfigurationOrNull();
if (configuration != null)
{
var appNameConfig = configuration["ApplicationName"];
if (!string.IsNullOrWhiteSpace(appNameConfig))
{
return appNameConfig;
}
}

var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
{
return entryAssembly.GetName().Name;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using JetBrains.Annotations;

namespace Volo.Abp;

public interface IApplicationNameAccessor
Expand All @@ -7,5 +9,6 @@ public interface IApplicationNameAccessor
/// This is useful for systems with multiple applications, to distinguish
/// resources of the applications located together.
/// </summary>
[CanBeNull]
string ApplicationName { get; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.Modularity.PlugIns;
using Xunit;
using IConfiguration = Castle.Core.Configuration.IConfiguration;

namespace Volo.Abp;

Expand Down Expand Up @@ -147,25 +153,58 @@ public void Should_Initialize_PlugIn()
[Fact]
public void Should_Set_And_Get_ApplicationName()
{
const string applicationName = "MyApplication";
var applicationName = "MyApplication";

using (var application = AbpApplicationFactory.Create<IndependentEmptyModule>(options =>
{
options.ApplicationName = applicationName;
}))
{
application.ApplicationName.ShouldBe(applicationName);
application.Services.GetApplicationName().ShouldBe(applicationName);

application.Initialize();


application.ServiceProvider
.GetRequiredService<IApplicationNameAccessor>()
.ApplicationName
.ShouldBe(applicationName);
}

using (var application = AbpApplicationFactory.Create<IndependentEmptyModule>(options =>
{
options.Services.ReplaceConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> {{"ApplicationName", applicationName}})
.Build());
}))
{

application.ApplicationName.ShouldBe(applicationName);
application.Services.GetApplicationName().ShouldBe(applicationName);

application.Initialize();

application.ServiceProvider
.GetRequiredService<IApplicationNameAccessor>()
.ApplicationName
.ShouldBe(applicationName);
}

applicationName = Assembly.GetEntryAssembly()?.GetName().Name;
using (var application = AbpApplicationFactory.Create<IndependentEmptyModule>())
{
application.ApplicationName.ShouldBe(applicationName);
application.Services.GetApplicationName().ShouldBe(applicationName);

application.Initialize();

application.ServiceProvider
.GetRequiredService<IApplicationNameAccessor>()
.ApplicationName
.ShouldBe(applicationName);
}
}

[Fact]
public async Task Should_Resolve_Root_Service_Provider()
{
Expand Down