R – 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?

Best Solution

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
        result = string.Format("You entered: {0}", value);


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

    return result;