Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Threading.Tasks;
using NServiceBus;
namespace SagaIndirectReplySample
{
class Program
{
static void Main(string[] args)
{
Start().GetAwaiter().GetResult();
}
static async Task Start()
{
var sagaEndpointConfig = new EndpointConfiguration("SagaEndpoint");
sagaEndpointConfig.UsePersistence<InMemoryPersistence>();
sagaEndpointConfig.SendFailedMessagesTo("error");
sagaEndpointConfig.Recoverability().Delayed(d => d.NumberOfRetries(0));
sagaEndpointConfig.Recoverability().Immediate(i => i.NumberOfRetries(0));
sagaEndpointConfig.LimitMessageProcessingConcurrencyTo(1);
var endpoint = await Endpoint.Start(sagaEndpointConfig);
while (true)
{
Console.WriteLine("Press <enter> to start a saga");
Console.ReadLine();
await endpoint.SendLocal(new StartSagaMessage() {UniqueId = Guid.NewGuid()});
}
}
}
class RequestHandler : IHandleMessages<RequestMessage>
{
public async Task Handle(RequestMessage message, IMessageHandlerContext context)
{
//Standard reply. Saga headers are copied by NServiceBus core
await context.Reply(new ReplyMessage()
{
UniqueId = message.UniqueId
});
//Reply sent via send (e.g. from yet another endpoint. Headers need to be copied manually
var options = new SendOptions();
options.SetHeader(Headers.SagaId, context.MessageHeaders[Headers.OriginatingSagaId]);
options.SetHeader(Headers.SagaType, context.MessageHeaders[Headers.OriginatingSagaType]);
options.SetHeader(Headers.MessageIntent, MessageIntentEnum.Reply.ToString());
options.RouteToThisEndpoint();
await context.Send(new IndirectReplyMessage()
{
UniqueId = message.UniqueId
}, options);
}
}
class MySaga : Saga<MySagaData>,
IAmStartedByMessages<StartSagaMessage>,
IHandleMessages<ReplyMessage>,
IHandleMessages<IndirectReplyMessage>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MySagaData> mapper)
{
mapper.ConfigureMapping<StartSagaMessage>(m => m.UniqueId).ToSaga(s => s.UniqueId);
}
public Task Handle(StartSagaMessage message, IMessageHandlerContext context)
{
return context.SendLocal(new RequestMessage()
{
UniqueId = Data.UniqueId
});
}
public Task Handle(ReplyMessage message, IMessageHandlerContext context)
{
Console.WriteLine($"Saga {Data.UniqueId} got reply with ID {message.UniqueId}.");
return Task.FromResult(0);
}
public Task Handle(IndirectReplyMessage message, IMessageHandlerContext context)
{
Console.WriteLine($"Saga {Data.UniqueId} got indirect reply with ID {message.UniqueId}.");
return Task.FromResult(0);
}
}
class ReplyMessage : IMessage
{
public Guid UniqueId { get; set; }
}
class IndirectReplyMessage : IMessage
{
public Guid UniqueId { get; set; }
}
class RequestMessage : IMessage
{
public Guid UniqueId { get; set; }
}
class StartSagaMessage : IMessage
{
public Guid UniqueId { get; set; }
}
class MySagaData : ContainSagaData
{
public Guid UniqueId { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment