Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

web services - WCF server-side timeout

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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().

Something like this:

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...