【问题标题】:XML-RPC timeout?XML-RPC 超时?
【发布时间】:2013-11-26 03:32:24
【问题描述】:

我正在尝试从 C# (.NET 3.5) 使用 XML-RPC 网络服务。如果它在 15 秒内没有响应,我希望请求超时,以便我可以尝试联系备份网络服务。

我正在使用CookComputing.XmlRpc 客户端。

【问题讨论】:

  • 您的意思是 C# 3 针对 .NET 3.5 运行吗?没有 C# 3.5 这样的东西。
  • 可能是以下内容的副本:stackoverflow.com/questions/10354275/…
  • 超时是通讯 API 的域,所以标记为 CookComputing 等。
  • 对于这样的用例也很有用:stackoverflow.com/questions/1370811/…
  • 在我们的系统中,我们设置了超时并重试 X 次,当 X = Y 时,我们尝试备份服务 - 唯一需要注意的是 Web 服务可能仍然处理请求超时。

标签: c# xml-rpc xml-rpc.net


【解决方案1】:

来自XML-RPC.NET docs:

2.4 如何设置代理方法调用的超时时间?

Proxy 类派生自 IXmlRpcProxy,因此继承了 Timeout 属性。这 接受一个整数,它以毫秒为单位指定超时。为了 例如,设置 5 秒超时:

ISumAndDiff proxy = XmlRpcProxyGen.Create<ISumAndDiff>();
proxy.Timeout = 5000;
SumAndDiffValue ret = proxy.SumAndDifference(2,3);

【讨论】:

    【解决方案2】:

    值得注意的是,这不适用于异步模型。为此,我会look at this post as this helped me overcome that problem.

    例如

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

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2018-12-28
      • 2014-09-13
      • 2013-09-12
      • 2014-06-13
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多