tags:

views:

38

answers:

2

Hi,

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

A: 

Hi,

This is not a duplicate entry. In the old post he is trying to achieve the synchronous call from the WCF function. Now he is trying to achieve synchronous call from the handle function of the generic host.

Thanks Dhanush

Dhanush
It's pretty well the same. The question still remains - why would you bother trying to turn a very good a-synchronous library into a horrible synchronous call? As you can tell by the difficulty you are having doing it, it shouldn't be done.
mrnye
+3  A: 

hi ajai,
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.

hacktick