通用主机中的同步发送/回复
我正在尝试使用通用主机 Windows 服务的处理程序函数的同步发送/回复,如下所示。但我认为NServiceBus只有在完成句柄功能后(在当前事务完成期间)才会发送消息。因此下面的代码将挂在“synchronousHandle.AsyncWaitHandle.WaitOne()”中。
这里最好的方法应该是什么?你能指导我吗...
Handler constructer
ConstructorFunction(bus)
{
Bus = bus
}
code in the handle function.
// sent the message to the bus and wait for the reply
IMessage response = null;
var synchronousHandle = Bus.Send(service2queue, requestMessage)
.Register(
(AsyncCallback)delegate(IAsyncResult asyncResult)
{
// Callback block for reply message
// Reply message received
NServiceBus.CompletionResult completionResult = asyncResult.AsyncState as NServiceBus.CompletionResult;
if (completionResult != null && completionResult.Messages.Length > 0)
{
// Always expecting one IMessage as reply
response = completionResult.Messages[0];
}
},
null);
// block the current thread till the reply received.
synchronousHandle.AsyncWaitHandle.WaitOne();
谢谢, 阿贾伊
I am trying to use synchronous send/reply from the handler function of the generic host windows service as below. But I think NServiceBus will send the message only after completing the handle function(during the current transaction complete). So below code will hang in ‘synchronousHandle.AsyncWaitHandle.WaitOne()’.
What should be the best approach here? Could you please guide me…
Handler constructer
ConstructorFunction(bus)
{
Bus = bus
}
code in the handle function.
// sent the message to the bus and wait for the reply
IMessage response = null;
var synchronousHandle = Bus.Send(service2queue, requestMessage)
.Register(
(AsyncCallback)delegate(IAsyncResult asyncResult)
{
// Callback block for reply message
// Reply message received
NServiceBus.CompletionResult completionResult = asyncResult.AsyncState as NServiceBus.CompletionResult;
if (completionResult != null && completionResult.Messages.Length > 0)
{
// Always expecting one IMessage as reply
response = completionResult.Messages[0];
}
},
null);
// block the current thread till the reply received.
synchronousHandle.AsyncWaitHandle.WaitOne();
Thanks,
Ajai
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当事情不应该做时,nservicebus 会尝试让事情变得尽可能困难。
来自 nservicebus 文档:
假设您位于服务器端(我在这里猜测是因为您向我们展示了一个消息处理程序)我会考虑重新设计。
如果您想在继续处理之前等待 service1 中的多条消息,请尝试考虑实现 sagas。
nservicebus tries to make things as hard as possible when they shouldn't be done.
from the nservicebus documentation:
assuming that you are on a server side (am guessing here because you showed us a messagehandler) i would considering a redesign.
if you want to wait for multiple messages in service1 before continuing the processing try considering to implement sagas.