【发布时间】:2012-01-30 09:25:52
【问题描述】:
我有一个命令处理程序,它调用域对象上的操作,该操作又在执行操作时触发一个事件。我想测试一个事件处理程序在发送相应命令时是否接收到事件(见下文,为简洁起见省略了一些代码)。事件处理程序 (MyEventConsumer.Consume) 永远不会被调用,即使事件消息在总线上发布(在这种情况下是环回总线)。有什么想法吗?
//Test
[TestFixture]
public class TestSendCommandReceiveEvent
{
[Given]
public void installation_of_infrastructure_objects()
{
container.Register(Component.For<MyEventConsumer>().UsingFactoryMethod(() => new MyEventConsumer(_received)));
container.Register(
Component.For<IServiceBus>()
.UsingFactoryMethod(() => ServiceBusFactory.New(x => { x.ReceiveFrom("loopback://localhost/mt_client"); x.Subscribe(conf => conf.LoadFrom(container)); })));
}
[When]
public void sending_a_command()
{
var LocalBus = container.Resolve<IServiceBus>();
LocalBus.Publish(new DoSomething(_aggregateId));
}
[Then]
public void corresponding_event_should_be_received_by_consumer()
{
_received.WaitOne(5000).ShouldBeTrue();
}
}
public class MyEventConsumer : Consumes<SomethingDone>.All
{
private readonly ManualResetEvent _received;
public MyEventConsumer(ManualResetEvent received)
{
_received = received;
}
public void Consume(SomethingDone message)
{
_received.Set();
}
}
//Command handler
public class DoSomethingCommandHandler : Consumes<DoSomething>.All where T:class
{
public void Consume(DoSomething message)
{
var ar = Repository.GetById<SomeAR>(message.ArId);
ar.DoSomething();
Repository.Save(ar, Guid.NewGuid(), null);
}
}
//Domain object
public class SomeDomainObject : AggregateBase
{
public void DoSomething()
{
RaiseEvent(new SomethingDone(Id, 1));
}
}
【问题讨论】:
-
这在生产中是否有效并且在测试中失败了?从代码看来,东西还可以,但我认为代码中存在一些错误,所以假设东西连接正确。我建议加入邮件列表,详细了解正在发生的事情。 groups.google.com/forum/#!forum/masstransit-discuss 如果我不得不猜测,也许这是容器的问题。我想我们都弄清楚了,但它可能是一个异常值。
-
嗯,似乎也是生产问题。一定是把总线配置错了。我去看看。
-
好的,看不出这里缺少什么(除了我自己缺乏 MT/Castle 经验)。转到邮件列表。
标签: c# masstransit