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.
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.
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);
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!");
}
}