Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 496
Meidan Alon Avatar asked Dec 07 '25 18:12

Meidan Alon


2 Answers

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;
}
like image 148
Maurice Avatar answered Dec 11 '25 01:12

Maurice


I have solved the same problem and created a blog post:

http://kanchengcao.blogspot.com/2012/06/adding-timeout-and-congestion.html

In short:

  1. WCF server timeout config do not work
  2. You could implement a timeout as others said
  3. Such a timeout does not guarantee a timely response to the client, since the request could be queued for long before entering your code with timeout.

So I implemented method to drop requests if the server is overloaded and is expected to cause more timeouts.

like image 27
Kancheng Cao Avatar answered Dec 11 '25 01:12

Kancheng Cao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!