Skip to content

Commit 15dd92d

Browse files
committed
Refactor persistence configuration to remove Settings dependency and streamline AssemblyLoadContextResolver usage
1 parent a268f50 commit 15dd92d

File tree

12 files changed

+44
-25
lines changed

12 files changed

+44
-25
lines changed

src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.Loader;
55
using System.Threading.Tasks;
66
using Hosting.Commands;
7+
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Hosting;
89
using Microsoft.Extensions.Logging.Abstractions;
910
using NServiceBus;
@@ -22,6 +23,8 @@ class StartupModeTests : AcceptanceTest
2223
[SetUp]
2324
public async Task InitializeSettings()
2425
{
26+
PersistenceFactory.AssemblyLoadContextResolver = static _ => AssemblyLoadContext.Default;
27+
2528
var transportIntegration = new ConfigureEndpointLearningTransport();
2629

2730
settings = new Settings
@@ -32,7 +35,6 @@ public async Task InitializeSettings()
3235
ErrorRetentionPeriod = TimeSpan.FromDays(1),
3336
PersistenceType = "RavenDB",
3437
ConnectionString = transportIntegration.ConnectionString,
35-
AssemblyLoadContextResolver = static _ => AssemblyLoadContext.Default
3638
}
3739
};
3840

@@ -50,7 +52,7 @@ public async Task CanRunMaintenanceMode()
5052
// not terminating.
5153
var hostBuilder = Host.CreateApplicationBuilder();
5254
settings.ServiceControl.MaintenanceMode = true;
53-
hostBuilder.Services.AddPersistence(hostBuilder.Configuration, settings); // TODO: Configuration needs to be initialized
55+
hostBuilder.Services.AddPersistence(hostBuilder.Configuration); // TODO: Configuration needs to be initialized
5456

5557
using var host = hostBuilder.Build();
5658
await host.StartAsync();

src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using NServiceBus.AcceptanceTesting.Support;
2222
using Particular.ServiceControl;
2323
using Particular.ServiceControl.Hosting;
24+
using Persistence;
2425
using RavenDB.Shared;
2526
using ServiceBus.Management.Infrastructure.Settings;
2627
using ServiceControl.Infrastructure;
@@ -51,10 +52,14 @@ async Task InitializeServiceControl(ScenarioContext context)
5152
Directory.CreateDirectory(logPath);
5253
var loggingSettings = new LoggingSettings
5354
{
54-
LogLevel = LogLevel.Debug, LogPath = logPath
55+
LogLevel = LogLevel.Debug,
56+
LogPath = logPath
5557
};
5658
LoggerUtil.ActiveLoggers = Loggers.Test;
5759

60+
PersistenceFactory.AssemblyLoadContextResolver = static _ => AssemblyLoadContext.Default;
61+
62+
5863
var settings = new Settings
5964
{
6065
Logging = loggingSettings,
@@ -101,8 +106,7 @@ async Task InitializeServiceControl(ScenarioContext context)
101106
}
102107

103108
return false;
104-
},
105-
AssemblyLoadContextResolver = static _ => AssemblyLoadContext.Default
109+
}
106110
}
107111
};
108112

src/ServiceControl.Transports/TransportFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ namespace ServiceControl.Transports
22
{
33
using System;
44
using System.IO;
5+
using System.Runtime.Loader;
6+
using Infrastructure;
57

68
public static class TransportFactory
79
{
10+
internal static Func<string, AssemblyLoadContext> AssemblyLoadContextResolver { get; set; } = static (assemblyPath) => new PluginAssemblyLoadContext(assemblyPath);
811
public static ITransportCustomization Create(TransportSettings settings)
912
{
1013
try
1114
{
1215
var transportManifest = TransportManifestLibrary.Find(settings.TransportType);
1316
var assemblyPath = Path.Combine(transportManifest.Location, $"{transportManifest.AssemblyName}.dll");
14-
var loadContext = settings.AssemblyLoadContextResolver(assemblyPath);
17+
var loadContext = AssemblyLoadContextResolver(assemblyPath);
1518
var customizationType = Type.GetType(transportManifest.TypeName, loadContext.LoadFromAssemblyName, null, true);
1619

1720
return (ITransportCustomization)Activator.CreateInstance(customizationType);

src/ServiceControl.Transports/TransportSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public class TransportSettings : SettingsHolder
88
{
9+
[Obsolete("",true)]
910
public Func<string, AssemblyLoadContext> AssemblyLoadContextResolver { get; set; }
1011

1112
public string TransportType { get; set; }

src/ServiceControl/HostApplicationBuilderExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ EndpointConfiguration configuration
7777
services.AddSingleton(provider => new Lazy<IMessageDispatcher>(provider.GetRequiredService<IMessageDispatcher>));
7878

7979
services.AddLicenseCheck();
80-
services.AddPersistence(hostBuilder.Configuration, settings);
80+
services.AddPersistence(hostBuilder.Configuration);
8181
services.AddMetrics(settings.ServiceControl.PrintMetrics);
8282

8383
NServiceBusFactory.Configure(settings, transportCustomization, transportSettings, configuration);
@@ -106,12 +106,13 @@ EndpointConfiguration configuration
106106
hostBuilder.AddServiceControlComponents(settings, transportCustomization, ServiceControlMainInstance.Components);
107107
}
108108

109-
public static void AddServiceControlInstallers(this IHostApplicationBuilder hostApplicationBuilder, Settings settings)
109+
public static void AddServiceControlInstallers(this IHostApplicationBuilder hostApplicationBuilder)
110110
{
111-
var persistence = PersistenceFactory.Create(hostApplicationBuilder.Configuration, settings);
111+
var persistence = PersistenceFactory.Create(hostApplicationBuilder.Configuration);
112112
persistence.AddInstaller(hostApplicationBuilder.Services);
113113
}
114114

115+
// TODO: Move to start
115116
static void RecordStartup(Settings settings, EndpointConfiguration endpointConfiguration)
116117
{
117118
var version = FileVersionInfo.GetVersionInfo(typeof(HostApplicationBuilderExtensions).Assembly.Location).ProductVersion;

src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ public static void SetupApplicationConfiguration(this IHostApplicationBuilder ho
7777
.AddLegacyAppSettings()
7878
.AddEnvironmentVariables();
7979

80+
// TODO: Add LoggingOptions/Settings
8081
hostBuilder.Services.AddOptions<PrimaryOptions>()
8182
.Bind(hostBuilder.Configuration.GetSection(PrimaryOptions.SectionName))
8283
.ValidateDataAnnotations()
8384
.ValidateOnStart();
85+
hostBuilder.Services.AddOptions<ServiceBusOptions>()
86+
.Bind(hostBuilder.Configuration.GetSection(ServiceBusOptions.SectionName))
87+
.ValidateDataAnnotations()
88+
.ValidateOnStart();
8489
}
8590
}
8691
}

src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public override async Task Execute(HostArguments args)
1414
{
1515
var hostBuilder = Host.CreateApplicationBuilder();
1616
hostBuilder.SetupApplicationConfiguration();
17-
var settings = hostBuilder.Configuration.Get<Settings>();
18-
hostBuilder.Services.AddPersistence(hostBuilder.Configuration, settings);
17+
hostBuilder.Services.AddPersistence(hostBuilder.Configuration);
1918

2019
if (WindowsServiceHelpers.IsWindowsService())
2120
{

src/ServiceControl/Hosting/Commands/SetupCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override async Task Execute(HostArguments args)
2020
hostBuilder.SetupApplicationConfiguration();
2121
var settings = hostBuilder.Configuration.Get<Settings>();
2222

23-
hostBuilder.AddServiceControlInstallers(settings);
23+
hostBuilder.AddServiceControlInstallers();
2424

2525
var componentSetupContext = new ComponentInstallationContext();
2626

src/ServiceControl/Infrastructure/Settings/Settings.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public void Configure(Settings options)
3737

3838
public class PrimaryOptions
3939
{
40+
4041
public const string SectionName = "ServiceControl";
4142

4243
public string TransportType { get; set; }
4344
public string PersistenceType { get; set; }
4445
public bool MaintenanceMode { get; set; }
4546
public ServiceControlSettings ServiceControlSettings { get; set; }
46-
[JsonIgnore] public Func<string, AssemblyLoadContext> AssemblyLoadContextResolver { get; set; } = static (assemblyPath) => new PluginAssemblyLoadContext(assemblyPath);
4747
public string NotificationsFilter { get; set; }
4848
public bool AllowMessageEditing { get; set; }
4949
public Func<MessageContext, bool> MessageFilter { get; set; } //HINT: acceptance tests only
@@ -97,8 +97,7 @@ public TransportSettings ToTransportSettings()
9797
ConnectionString = ConnectionString,
9898
MaxConcurrency = MaximumConcurrencyLevel,
9999
RunCustomChecks = true,
100-
TransportType = TransportType,
101-
AssemblyLoadContextResolver = AssemblyLoadContextResolver
100+
TransportType = TransportType
102101
};
103102
return transportSettings;
104103
}

src/ServiceControl/Infrastructure/Settings/SettingsConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void PostConfigure(string name, PrimaryOptions options)
159159
options.ConnectionString ??= System.Configuration.ConfigurationManager.ConnectionStrings["NServiceBus/Transport"]?.ConnectionString;
160160
options.InstanceId = InstanceIdGenerator.FromApiUrl(options.ApiUrl);
161161

162-
options.RemoteInstanceSettings= string.IsNullOrEmpty(options.RemoteInstances)
162+
options.RemoteInstanceSettings = string.IsNullOrEmpty(options.RemoteInstances)
163163
? []
164164
: ParseRemoteInstances(options.RemoteInstances);
165165
}

0 commit comments

Comments
 (0)