.NET webservice - acknowledge quickly, but continue processing in background

℡╲_俬逩灬. 提交于 2019-12-04 11:21:38

I think 1st is the only possible solution. 2nd is very bad design. Assume you process each request for 1 second, and there are 2 requests coming in 1 second in average. You will quickly run out of resources (because more and more threads will be used)

1st one is good because you have all control on how you will store and process requests. Good design is to have web service as frontend that will just answer "Success" and enqueue requests. As a persistent storage i recommend MSMQ or database. Then create a service app that will have pool of threads and will pick from queue(db).

  1. Receive the request
  2. Spin off background process (e.g. with a BackgroundWorker) with the passed-in parameters
  3. Return an acknowledgement - with a Guid or similar identifier of the request
  4. Do your processing ...
    1. In the meantime, the caller can ping your service for a status update based on the identifier
    2. While the processing is happening, return either a "still working" response or, if you're clever or can accurately predict it, return a progress measure (maybe as a percentage done or a quantity of time left)
  5. When the processing is done, return a "success" message to the status request
  6. Make the solution available, via the identifier you replied with earlier

Option 1 is the way to go, as the application already supports queueing.

As for an option 2 - you can start a worker thread from you web service, this will process the work and you can proceed to return "success" from the main thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!