public void SubscribeWithHandler(Func <TransportMessage, CancellationToken, Task> handler) { if (_bus != null) { return; } _bus = Configure .With(_activator) .Options(o => o.SetNumberOfWorkers(0)) .Routing(r => r.AddTransportMessageForwarder(async transportMessage => { if (!AllHeadersMatch(transportMessage.Headers)) { return(ForwardAction.None); } await handler(transportMessage, CancellationToken.None); return(ForwardAction.Ignore()); })) .Transport(t => { t.UseRabbitMq(Configuration.ConnectionString, $"Elsa{Guid.NewGuid().ToString("n").ToUpper()}").InputQueueOptions(o => o.SetAutoDelete(autoDelete: true)); }) .Start(); _bus.Advanced.Topics.Subscribe(Configuration.RoutingKey); }
public void SubscribeWithHandler(Func <TransportMessage, CancellationToken, Task> handler) { if (_bus != null) { return; } _bus = Configure .With(_activator) .Routing(r => r.AddTransportMessageForwarder(async transportMessage => { if (!AllHeadersMatch(transportMessage.Headers)) { return(ForwardAction.None); } await handler(transportMessage, CancellationToken.None); return(ForwardAction.Ignore()); })) .Transport(t => { t.UseRabbitMq(Configuration.ConnectionString, Configuration.ClientId).InputQueueOptions(x => x.SetAutoDelete(Configuration.AutoDeleteQueue)); }) .Start(); _bus.Advanced.Topics.Subscribe(Configuration.TopicFullName); }
protected override Task ExecuteAsync(CancellationToken stoppingToken) { var handlerActivator = new DependencyInjectionHandlerActivator(_serviceProvider); var configurer = Configure.With(handlerActivator); configurer .Serialization(serializer => serializer.UseNewtonsoftJson(DefaultContentSerializer.CreateDefaultJsonSerializationSettings())) .Transport(transport => transport.UseAzureServiceBus(_configuration.GetConnectionString("AzureServiceBus"), "error")) .Routing(r => { r.AddTransportMessageForwarder(transportMessage => { var returnAddress = transportMessage.Headers[Headers.SourceQueue]; if (returnAddress.Contains("workflow-management-events")) { return(Task.FromResult(ForwardAction.Ignore())); } return(Task.FromResult(ForwardAction.ForwardTo(returnAddress))); }); }); var bus = configurer.Start(); return(Task.CompletedTask); }
public async Task CanIgnoreMessageCompletely(bool ignoreTheMessage) { var network = new InMemNetwork(); var activator = new BuiltinHandlerActivator(); Using(activator); var gotTheMessage = new ManualResetEvent(false); activator.Handle <string>(async str => { gotTheMessage.Set(); }); var recipients = new[] { "recipient-A", "recipient-B" }.ToList(); recipients.ForEach(network.CreateQueue); Configure.With(activator) .Transport(t => t.UseInMemoryTransport(network, "forwarder")) .Routing(t => { if (ignoreTheMessage) { t.AddTransportMessageForwarder(async transportMessage => ForwardAction.Ignore()); } }) .Start(); await activator.Bus.SendLocal("HEJ MED DIG!!!"); if (ignoreTheMessage) { Assert.That(gotTheMessage.WaitOne(TimeSpan.FromSeconds(0.5)), Is.False); } else { Assert.That(gotTheMessage.WaitOne(TimeSpan.FromSeconds(0.5)), Is.True); } }