-2

I'm attempting to consume an XML-RPC webservice from C# (.NET 3.5). If it doesn't respond within 15 seconds, I'd like the request to time out, so that I can attempt to contact the backup webservice.

I am using the CookComputing.XmlRpc client.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
André Luiz
  • 6,642
  • 9
  • 55
  • 105
  • 5
    Do you mean C# 3 running against .NET 3.5? There's no such thing as C# 3.5. – Jon Skeet Nov 13 '13 at 13:42
  • possibly a duplicate of: http://stackoverflow.com/questions/10354275/c-sharp-how-to-stop-a-method-if-it-takes-longer-than-2-seconds?rq=1 – mao47 Nov 13 '13 at 13:45
  • Timeouts are the domain of the comms API, so tag it for CookComputing etc. – H H Nov 13 '13 at 13:46
  • 1
    Also useful for such a use case: http://stackoverflow.com/questions/1370811/implementing-a-timeout-on-a-function-returning-a-value – Vinzz Nov 13 '13 at 13:48
  • In our system, we set a time-out and retry X times, when X = Y, we try a backup service - the only thing to watch out for is that the web service may still process a timed out request. – Mr Shoubs Nov 13 '13 at 13:48

2 Answers2

4

From the XML-RPC.NET docs:

2.4 How do I set a timeout on a proxy method call?

Proxy classes are derived from IXmlRpcProxy and so inherit a Timeout property. This takes an integer which specifies the timeout in milliseconds. For example, to set a 5 second timeout:

ISumAndDiff proxy = XmlRpcProxyGen.Create<ISumAndDiff>();
proxy.Timeout = 5000;
SumAndDiffValue ret = proxy.SumAndDifference(2,3);
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
1

It might be worth to note that this doesn't work with an async model. To do this I would look at this post as this helped me overcome that problem.

For example

public interface IAddNumbersContract
{
    [XmlRpcBegin("add_numbers")]
    IAsyncResult BeginAddNumbers(int x, int y, AsyncCallback acb);

    [XmlRpcEnd]
    int EndAddNumbers(IAsyncResult asyncResult);
}

public class AddNumbersCaller
{
    public async Task<int> Add(int x, int y)
    {
        const int timeout = 5000;
        var service = XmlRpcProxyGen.Create<IAddNumbersContract>();
        var task = Task<int>.Factory.FromAsync((callback, o) => service.BeginAddNumbers(x, y, callback), service.EndAddNumbers, null);
        if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
        {
            return task.Result;
        }

        throw new WebException("It timed out!");
    }
}
Community
  • 1
  • 1