Using ThreadPool.QueueUserWorkItem in WCF method

891 Views Asked by At

I need to run a background logic that takes around 25-30 sec inside a WCF method that can't take more than 1 sec to complete. I've decided to wrap that logic into a WaitCallback and pass it to ThreadPool.QueueUserWorkItem right before I exit the web method. Initially it worked ok but now I'm having second thoughts because I suspect that sometimes QueueUserWorkItem method doesn't return in a timely manner as a result web method doesn't respond within 1 sec on a regular basis. Are there any issues with using QueueUserWorkItem inside WCF methods?

1

There are 1 best solutions below

0
On

No not as such, but your question touches upon a more general problem, what to do with long-running service calls? You can either:

  1. Change the configs so that client and server tolerate long service calls, i.e. increase timeouts
  2. Or, design your service calls with a start / get current progress / get final result API, all of which return quickly:

    int jobID = serviceProxy.StartJob();
    float progress = serviceProxy.GetJobProgress(int jobID);
    Result finalResult = serviceProxy.GetJobResult(int jobID);
    

This is more work, but a better design, and you now also have to maintain a list of running jobs (your async proceessing which could use QueueUserWorkItem or whatever), but all the service calls would return quickly.