Skip to content

Commit

Permalink
Merge pull request #10 from Phenek/master
Browse files Browse the repository at this point in the history
Allow custom/multi hosted mqttServer
  • Loading branch information
maikebing authored Dec 31, 2023
2 parents 22af0e0 + 748e1c3 commit 4649da9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,7 @@ ASALocalRun/
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb
healthchecksdb

# VsCode
.vscode/
9 changes: 5 additions & 4 deletions Source/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public static IServiceCollection AddMqttControllers(this IServiceCollection serv
return MqttRouteTableFactory.Create(assemblies);
});

services.AddSingleton<ITypeActivatorCache>(new TypeActivatorCache());
services.AddSingleton<MqttRouter>();
services.AddTransient<MqttRouter>();
if (_opt.RouteInvocationInterceptor != null)
{
services.AddSingleton(typeof(IRouteInvocationInterceptor), _opt.RouteInvocationInterceptor);
Expand Down Expand Up @@ -98,6 +98,7 @@ public static IApplicationBuilder UseAttributeRouting(this IApplicationBuilder a
{
var router = app.ApplicationServices.GetRequiredService<MqttRouter>();
var server = app.ApplicationServices.GetRequiredService<MqttServer>();
router.Server = server;
var interceptor = app.ApplicationServices.GetService<IRouteInvocationInterceptor>();
server.InterceptingPublishAsync += async (args) =>
{
Expand Down Expand Up @@ -128,11 +129,11 @@ public static IApplicationBuilder UseAttributeRouting(this IApplicationBuilder a
return app;
}

[Obsolete("Use UseAttributeRouting instead")]
public static void WithAttributeRouting(this MqttServer server, IServiceProvider svcProvider, bool allowUnmatchedRoutes = false)
{
var router = svcProvider.GetRequiredService<MqttRouter>();
var interceptor = svcProvider.GetRequiredService<IRouteInvocationInterceptor>();
router.Server = server;
var interceptor = svcProvider.GetService<IRouteInvocationInterceptor>();
server.InterceptingPublishAsync += async (args) =>
{
object correlationObject = null;
Expand Down
6 changes: 3 additions & 3 deletions Source/Routing/MqttRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal class MqttRouter
private readonly MqttRouteTable routeTable;
private readonly ITypeActivatorCache typeActivator;

public MqttServer? Server { get; set; }

public MqttRouter(ILogger<MqttRouter> logger, MqttRouteTable routeTable, ITypeActivatorCache typeActivator)
{
this.logger = logger;
Expand Down Expand Up @@ -76,7 +78,6 @@ internal async Task OnIncomingApplicationMessage(IServiceProvider svcProvider, I
})
.ToArray();


if (activateProperties.Length == 0)
{
logger.LogDebug($"MqttController '{declaringType.FullName}' does not have a property that can accept a controller context. You may want to add a [{nameof(MqttControllerContextAttribute)}] to a pubilc property.");
Expand All @@ -85,7 +86,7 @@ internal async Task OnIncomingApplicationMessage(IServiceProvider svcProvider, I
var controllerContext = new MqttControllerContext()
{
MqttContext = context,
MqttServer = scope.ServiceProvider.GetRequiredService<MqttServer>()
MqttServer = Server
};

for (int i = 0; i < activateProperties.Length; i++)
Expand Down Expand Up @@ -152,7 +153,6 @@ internal async Task OnIncomingApplicationMessage(IServiceProvider svcProvider, I
}
}
}

}

private static Task HandlerInvoker(MethodInfo method, object instance, object?[]? parameters)
Expand Down

0 comments on commit 4649da9

Please sign in to comment.