通用主机中的同步发送/回复

发布于 2024-09-30 05:26:50 字数 1631 浏览 3 评论 0原文

我正在尝试使用通用主机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-10-07 05:26:51

当事情不应该做时,nservicebus 会尝试让事情变得尽可能困难。

来自 nservicebus 文档:

Bus.Send(request).Register(asyncCallback, state)

Callback only fires on first response, then is cleaned up to prevent memory leaks. 
Doesn’t survive restarts – not suitable for server-side

假设您位于服务器端(我在这里猜测是因为您向我们展示了一个消息处理程序)我会考虑重新设计。

service1 gets a notification about messageA
service1 sends message requestMessage to service2
service2 replies with message responseMessage to service1
service1 handles responseMessage and continues processing

如果您想在继续处理之前等待 service1 中的多条消息,请尝试考虑实现 sagas。

nservicebus tries to make things as hard as possible when they shouldn't be done.

from the nservicebus documentation:

Bus.Send(request).Register(asyncCallback, state)

Callback only fires on first response, then is cleaned up to prevent memory leaks. 
Doesn’t survive restarts – not suitable for server-side

assuming that you are on a server side (am guessing here because you showed us a messagehandler) i would considering a redesign.

service1 gets a notification about messageA
service1 sends message requestMessage to service2
service2 replies with message responseMessage to service1
service1 handles responseMessage and continues processing

if you want to wait for multiple messages in service1 before continuing the processing try considering to implement sagas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文