Skip to content

Instantly share code, notes, and snippets.

@andreasohlund
Last active June 21, 2019 08:06
Show Gist options
  • Save andreasohlund/828c2c97f93af6d9cbcc4e2455127e45 to your computer and use it in GitHub Desktop.
Save andreasohlund/828c2c97f93af6d9cbcc4e2455127e45 to your computer and use it in GitHub Desktop.
var routerConfiguation = new RouterConfiguration("Router");
routerConfiguation.AddInterface<MsmqTransport>("MSMQ").DisableMessageDrivenPublishSubscribe();
routerConfiguation.AddInterface<SqlServerTransport>("SQL", t => t.ConnectionString(myConnectionString)).DisableMessageDrivenPublishSubscribe();
routerConfiguation.Chains.AddRule(c => new MetricsPreroutingTerminator("MSMQ", "Particular.Monitoring@some-machine"));
routerConfiguation.UseStaticRoutingProtocol();
var router = Router.Create(routerConfiguation);
await router.Start().ConfigureAwait(false);
class MetricsPreroutingTerminator : ChainTerminator<PreroutingContext>
{
string metricsEndpoint;
string metricsInterface;
public MetricsPreroutingTerminator(string metricsInterface, string metricsEndpoint)
{
this.metricsEndpoint = metricsEndpoint;
this.metricsInterface = metricsInterface;
}
protected override async Task<bool> Terminate(PreroutingContext context)
{
if (context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes)
&& messageTypes == "NServiceBus.Metrics.EndpointMetadataReport")
{
var interfaces = context.Extensions.Get<IInterfaceChains>();
await interfaces.GetChainsFor(metricsInterface).Get<AnycastContext>()
.Invoke(new AnycastContext(metricsEndpoint, new OutgoingMessage(context.MessageId, context.Headers.Copy(), context.Body), DistributionStrategyScope.Send, context))
.ConfigureAwait(false);
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment