Search code examples
azureazureservicebusazure-servicebusrelay

Does azure service bus relay support async wcf opertions?


Does service bus relay support async wcf operations?

I get the server failed to return a meaningful response with the following code. If I change the timespan to 30 seconds though it works fine.

started by following this tutorial http://azure.microsoft.com/en-us/documentation/articles/service-bus-dotnet-how-to-use-relay

Client code:

var task = Task<string>.Factory.FromAsync(channel.BeginDoEcho, channel.EndDoEcho, input, null);
Console.WriteLine("Server echoed: {0}", task.Result );

Server code:

public IAsyncResult BeginDoEcho(string text, AsyncCallback callback, object state)
{
    var task = Task<string>.Factory.StartNew(x =>
    {
        Thread.Sleep(TimeSpan.FromMinutes(5));
        return text;
    }, state);
    return task.ContinueWith(result => callback(task));
}

public string EndDoEcho(IAsyncResult result)
{
    return ((Task<string>) result).Result;
}

Solution

  • Azure cannot tell whether you have implemented your service synchronously or asynchronously. This is an implementation detail that is not exposed on-the-write. Whatever the reason for your problems is - it's not that the remote side communicating with your service is affect by async.

    In fact you can decide independently for client and server whether you want to use async or not.

    With the code given you should always see a timeout error if the timeout is below 5 minutes since the server takes 5 minutes to answer. It is not the case that the server immediately returns an IAsyncResult that completes after 5 minutes. IAsyncResult is not serializable so it never goes over the wire. Nothing at all is sent for 5 minutes.

    Unrelated to this question: Use await to implement asynchrony. Much easier.

    Your current server implementation has a problem: You are blocking a thread for 5 minutes with the synchronous sleep. That completely negates the benefits of async. If you have many such operations executing concurrently this will lead to thread-pool exhaustion.