我想问一下,当我为 Request/Reply 做 saga 时,我是否理解正确,感谢这个https://github.com/rebus-org/Rebus/issues/105我可以ConfigureHowToFindSaga
留空?
如果是真的,最好在 Wiki 页面中提及它,以及在适配器中注册 saga 的需要 :)
下面的代码似乎可以工作,请您看看是否可以?
namespace ConsoleApplication1
{
class Program
{
static IBus bus;
static void Main(string[] args)
{
var adapter = new BuiltinContainerAdapter();
adapter.Register<SampleSaga>(() => new SampleSaga(bus));
bus = Configure.With(adapter)
.Logging(l => l.ColoredConsole())
.Transport(t => t.UseMsmq("enpoint1", "endpoint1_errors"))
.MessageOwnership(x => x.FromRebusConfigurationSection())
.Sagas(x => x.StoreInMemory())
.CreateBus()
.Start();
bus.SendLocal(new SampleMessage() { Test = "Hi there" });
}
}
class SampleSaga : Saga<MessageHolder<SampleMessage>>,
IAmInitiatedBy<SampleMessage>,
IHandleMessages<Response>
{
IBus bus;
public SampleSaga(IBus bus)
{
this.bus = bus;
}
public override void ConfigureHowToFindSaga()
{
}
public void Handle(SampleMessage message)
{
Data.Message = message;
bus.Send(new Request());
}
public void Handle(Response message)
{
Console.Write(string.Format("Response arrived. Holded message: {0}", Data.Message.Test));
MarkAsComplete();
}
}
class MessageHolder<T> : ISagaData
{
public Guid Id { get; set; }
public int Revision { get; set; }
public T Message { get; set; }
}
class SampleMessage
{
public string Test { get; set; }
}
}
namespace ConsoleApplication2
{
class Program
{
static IBus bus;
static void Main(string[] args)
{
var adapter = new BuiltinContainerAdapter();
adapter.Handle<Request>(x => bus.Reply(new Response()));
bus = Configure.With(adapter)
.Logging(l => l.ColoredConsole())
.Transport(t => t.UseMsmq("endpoint2", "endpoint2_errors"))
.MessageOwnership(x => x.FromRebusConfigurationSection())
.CreateBus()
.Start();
}
}
public class Request
{
}
public class Response
{
}
}