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..d9d9fdb 100644 --- a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -20,14 +20,27 @@ namespace MQTTnet.AspNetCore return services; } - + public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure) { - var builder = new MqttServerOptionsBuilder(); + services.AddSingleton(s => { + var builder = new MqttServerOptionsBuilder(); + configure(builder); + return builder.Build(); + }); - configure(builder); - - services.AddSingleton(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); + return builder.Build(); + }); services.AddHostedMqttServer();