Skip to content

Instantly share code, notes, and snippets.

@SzymonPobiega
Created April 3, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SzymonPobiega/a400faf153aa0151ecd7390ba096ebb3 to your computer and use it in GitHub Desktop.
Save SzymonPobiega/a400faf153aa0151ecd7390ba096ebb3 to your computer and use it in GitHub Desktop.
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