WCF 服务器端超时 [英] WCF server-side timeout

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

问题描述

有没有办法告诉 WCF 服务在一段时间后响应请求(无论是否中止它的处理),即使它还没有完成,比如服务器端超时策略?

Is there a way to tell a WCF service to response to a request (with or without aborting it's processing) after a certain amount of time, even if it didn't finish yet, something like a server-side timeout policy?

推荐答案

我想您可以通过在 WCF 操作开始后立即启动一个新线程来实现.然后真正的工作发生在新线程上,原始 WCF 请求线程使用 Thread.Join() 等待特定超时.如果发生超时,可以使用 Thread.Abort() 取消工作线程.

I suppose you could do this by starting a new Thread as soon as the WCF operation starts. The real work then happens on the new thread and the original WCF request thread waits using a Thread.Join() with a specific timeout. If the timeout occurs the worker thread can be canceled using a Thread.Abort().

像这样:

public string GetData(int value)
{
    string result = "";
    var worker = new Thread((state) =>
    {
        // Simulate l0ng running
        Thread.Sleep(TimeSpan.FromSeconds(value));
        result = string.Format("You entered: {0}", value);
    });

    worker.Start();

    if (!worker.Join(TimeSpan.FromSeconds(5)))
    {
        worker.Abort();
        throw new FaultException("Work took to long.");
    }

    return result;
}

这篇关于WCF 服务器端超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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