From 4cc9f12a03bf9359f0c2c094ec6807ab84367db2 Mon Sep 17 00:00:00 2001 From: JanEggers Date: Tue, 21 Aug 2018 21:32:03 +0200 Subject: [PATCH 1/2] added AspNetMqttServerOptionsBuilder --- .../AspNetMqttServerOptionsBuilder.cs | 15 +++++++++++++++ .../ServiceCollectionExtensions.cs | 14 +++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 Source/MQTTnet.AspnetCore/AspNetMqttServerOptionsBuilder.cs diff --git a/Source/MQTTnet.AspnetCore/AspNetMqttServerOptionsBuilder.cs b/Source/MQTTnet.AspnetCore/AspNetMqttServerOptionsBuilder.cs new file mode 100644 index 0000000..e0ae333 --- /dev/null +++ b/Source/MQTTnet.AspnetCore/AspNetMqttServerOptionsBuilder.cs @@ -0,0 +1,15 @@ +using MQTTnet.Server; +using System; + +namespace MQTTnet.AspNetCore +{ + public class AspNetMqttServerOptionsBuilder : MqttServerOptionsBuilder + { + public AspNetMqttServerOptionsBuilder(IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + } + + public IServiceProvider ServiceProvider { get; } + } +} diff --git a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index b5d0440..45a0df5 100644 --- a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -21,13 +21,13 @@ namespace MQTTnet.AspNetCore return services; } - public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) - { - var builder = new MqttServerOptionsBuilder(); - - configure(builder); - - services.AddSingleton(builder.Build()); + public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) + { + services.AddSingleton(s => { + var builder = new AspNetMqttServerOptionsBuilder(s); + configure(builder); + return builder.Build(); + }); services.AddHostedMqttServer(); From 976c62d9c2def88e8220fa7b6af8ca93201c6fb0 Mon Sep 17 00:00:00 2001 From: JanEggers Date: Sat, 25 Aug 2018 12:08:42 +0200 Subject: [PATCH 2/2] added back the old function and added new overload --- .../ServiceCollectionExtensions.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index 45a0df5..d9d9fdb 100644 --- a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -20,9 +20,22 @@ namespace MQTTnet.AspNetCore return services; } - - public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) - { + + public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) + { + services.AddSingleton(s => { + var builder = new MqttServerOptionsBuilder(); + configure(builder); + return builder.Build(); + }); + + services.AddHostedMqttServer(); + + return services; + } + + public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action configure) + { services.AddSingleton(s => { var builder = new AspNetMqttServerOptionsBuilder(s); configure(builder);