diff --git a/Source/MQTTnet.Tests/Mockups/AspNetCoreTestEnvironment.cs b/Source/MQTTnet.Tests/Mockups/AspNetCoreTestEnvironment.cs index 85d434023..1342a7943 100644 --- a/Source/MQTTnet.Tests/Mockups/AspNetCoreTestEnvironment.cs +++ b/Source/MQTTnet.Tests/Mockups/AspNetCoreTestEnvironment.cs @@ -36,14 +36,7 @@ public override IMqttClient CreateClient() { var services = new ServiceCollection(); var clientBuilder = services.AddMqttClient(); - if (EnableLogger) - { - clientBuilder.UseAspNetCoreMqttNetLogger(); - } - else - { - clientBuilder.UseMqttNetNullLogger(); - } + UseLogger(clientBuilder); var s = services.BuildServiceProvider(); var client = s.GetRequiredService().CreateMqttClient(); @@ -84,7 +77,8 @@ public override async Task StartServer(Action + + var serverBuilder = appBuilder.Services.AddMqttServer(optionsBuilder => { optionsBuilder.WithDefaultEndpoint(); optionsBuilder.WithDefaultEndpointPort(ServerPort); @@ -93,19 +87,20 @@ public override async Task StartServer(Action k.ListenMqtt()); appBuilder.Host.ConfigureHostOptions(h => h.ShutdownTimeout = TimeSpan.FromMilliseconds(500d)); _app = appBuilder.Build(); - - // The OS has chosen the port to we have to properly expose it to the tests. - ServerPort = _app.Services.GetRequiredService().DefaultEndpointOptions.Port; - await _app.StartAsync(); + Server = _app.Services.GetRequiredService(); return Server; } @@ -126,17 +121,18 @@ public override async Task StartServer(MqttServerOptionsBuilder opti optionsBuilder.WithDefaultEndpointPort(ServerPort); optionsBuilder.WithMaxPendingMessagesPerClient(int.MaxValue); - var options = optionsBuilder.Build(); - var appBuilder = WebApplication.CreateBuilder(); - appBuilder.Services.AddMqttServer(); - appBuilder.Services.AddSingleton(options); + appBuilder.Services.AddSingleton(optionsBuilder.Build()); + var serverBuilder = appBuilder.Services.AddMqttServer(); + + UseLogger(serverBuilder); appBuilder.WebHost.UseKestrel(k => k.ListenMqtt()); appBuilder.Host.ConfigureHostOptions(h => h.ShutdownTimeout = TimeSpan.FromMilliseconds(500d)); _app = appBuilder.Build(); await _app.StartAsync(); + Server = _app.Services.GetRequiredService(); return Server; } @@ -164,5 +160,17 @@ private static int GetServerPort() } return port; } + + private void UseLogger(IMqttBuilder builder) + { + if (EnableLogger) + { + builder.UseAspNetCoreMqttNetLogger(); + } + else + { + builder.UseMqttNetNullLogger(); + } + } } } \ No newline at end of file