为特定的Web服务方法设置超时 [英] Set timeout for particular method of web service

查看:70
本文介绍了为特定的Web服务方法设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的Windows应用程序中有Web服务引用,这个Web服务有很多方法。我想为不同的方法设置不同的超时时间。

例如方法A设置TimeOut = 10秒

方法B设置TimeOut = 20秒



提前致谢:)



我的尝试:



Hi ,

I am having web service reference in my windows application and this web service having many methods . I want to set the different time out for different methods.
for e.g methodA set TimeOut = 10 sec
methodB set TimeOut = 20 sec

Thanks in advance :)

What I have tried:

Hi ,

I am having web service reference in my windows application  and this web service having many methods . I want to set the different  time out for different methods.
for e.g  methodA set TimeOut = 10 sec
         methodB set TimeOut = 20 sec

Thanks in advance :)

推荐答案

您无法设置超时在每个方法的基础上,但可以做类似于此链接描述的内容:



制作Task.TimeoutAfter方法|使用.NET并行编程 [ ^ ]



我的谷歌搜索是c#timeout with task。



这里有一些可能适合你的最小代码:



You can't set a timeout on a per-method basis, but could do something like what is described at this link:

Crafting a Task.TimeoutAfter Method | Parallel Programming with .NET[^]

My google search was "c# timeout with task".

Here's some minimal code that may work for you:

/// <summary>
/// Times out if the task execution takes longer that the specified number of milliseconds.
/// Up to 596.5 hours of timeout can be specified.
/// </summary>
/// <param name="task">The task to execute</param>
/// <param name="msTimeout">How long to wait for the task to return (&lt;= 0 causes
/// this value to be = int.MaxValue.</param>
/// <returns></returns>
public static async Task TimeoutAfter(this Task task, int msTimeout)
{
    // make sure we have a sane timout value
    msTimeout = (msTimeout <= 0) ? int.MaxValue : msTimeout;
    if (task == await Task.WhenAny(task, Task.Delay(msTimeout)))
    {
        await task;
    }
    else
    {
        throw new TimeoutException();
    }
}


这篇关于为特定的Web服务方法设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆