Today I released v0.3.0
of Routemeister, where I added a new router that allows you to hook in middlewares in a way similar to how you can to in Owin. This means, you e.g. can intercept, wrap or exit the pipeline before the message is routed to its handler.
Use the new router
First lets get som routes:
var factory = new MessageRouteFactory();
var routes = factory.Create(fromAssembly, typeof(IMyHandler<>));
Now, instead of SequentialAsyncMessageRouter
you use MiddlewareEnabledAsyncMessageRouter
:
var router = new MiddlewareEnabledAsyncMessageRouter((t, e) =>
Activator.CreateInstance(t)
routes);
router.Use(next => async msgEnvelope =>
{
try
{
_log.Debug("Before");
await next(msgEnvelope );
_log.Debug("After");
}
catch (Exception ex)
{
_log.Error(ex);
}
});
router.Use(next => async msgEnvelope =>
{
if(msgEnvelope.MessageType == typeof(Foo))
return;
await next(msgEnvelope );
});
Now, whenever you route a message, it will be passed to the middlewares:
await router.RouteAsync(new MyMessage { Score = 42 });
You don't need a router
Remember, you don't have to use a router to use Routemeister. All you need are the routes:
var route = routes.GetRoute(messageType);
var routeAction = route.Actions.Single();
var messageEnvelope = new MessageEnvelope(myMessage, messageType);
await routeAction.Invoke(handler, messageEnvelope);
That's it. You can read more about a use-case in where this is used with Autofac, to create child lifetime scopes, and thereby per-request resource scoped messages being routed.
//Daniel