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?
Asked
Active
Viewed 880 times
1
-
ThreadPool.QueueUserWorkItem is bit outmoded. You should consider using [TPL](http://stackoverflow.com/questions/9200573/threadpool-queueuserworkitem-vs-task-factory-startnew). +1 for Chris' response. – Petar Vučetin Apr 25 '12 at 02:35
1 Answers
1
No not as such, but your question touches upon a more general problem, what to do with long-running service calls? You can either:
- Change the configs so that client and server tolerate long service calls, i.e. increase timeouts
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.

Chris O
- 5,017
- 3
- 35
- 42