开发者

Web Services. Get input data, process it at background thread

开发者 https://www.devze.com 2023-01-06 18:39 出处:网络
I\'ve got several web-services: asmx,wcf. At couple of them there are some methods, which take a lot of time for processing, but size of input data for t开发者_开发问答hese methods are small and it ta

I've got several web-services: asmx,wcf. At couple of them there are some methods, which take a lot of time for processing, but size of input data for t开发者_开发问答hese methods are small and it takes not much time to transfer on the wire. I want move to not sync model. Client passes data to service, service answers that data transfer was correct and process it at background thread witout connection with client. So agter transfering connection should be closed. IS it possible? Can u help me with articles or may be just google request.


John is right - Once you close an http connection, it is done. You can't get back to the same process.

So if you can use another technology that allows duplex on one connection (e.g. WCF), do it!

However,

if you have no choice but to use webservices,

here are three ways to make it work. You may get timeouts on any of them.

Option 1:

Forget the part about 'client answers data was correct.' Just have each thread make its request and wait for the data.

Option 2:

Now, assuming that won't work and you must do the validation, this way requires the client to make 2 requests.

First request: returns valid/invalid. Second request: returns the long-running results.

Variation of option 2:

If you have timeout problems, you could have the first request generate a GUID or unique database key and start another process, passing it this key, and return the key to the client. (if you can get the server to allow you to start a process - depends on security settings/needs - if not you may be able to start an async thread and have it keep running after the websvc one ends?) The process will do the long task, update the row in the database w/ the unique id when finished, revealing the results plus a 'done' flag. The second request by the client could always return immediately and if the processing is not done, return that, if it is, return the results. The client will repeat this every 5 sec or so until done.

Hacks, I know, but we don't always have a choice for the technology we use.


Don't do this with ASMX web services. They weren't designed for that. If you must do it with ASMX, then have the ASMX pass the data off to a Windows Service that will do the actual work, in the background.

This is more practical with WCF.


We have been writing stuff to interact with the UK gov website and the way they handle something similar is that you send your request and data to the server and it responds saying, roughly, "thanks very much - we're processing it now, please call back later using this id" - all in an XML message. You then, at some point later, send a new http request to the service saying, essentially, "I'm enquiring about the status of this particular request id" and the server returns a result that says either it has processed OK, or processed with errors, or is still processing, please try again in xx seconds.
Similar to option 2 described previously.
It's a polling solution rather than a callback or 2 way conversation but it seems to work.

The server will need to keep, or have access to, some form of persistent table or log for each request state - it can contain eg, the id, the original request, current stage through the workflow, any error messages so far, the result (if any) etc. And the web service should probably have passed the bulk of the request off to a separate Windows service as already mentioned.

0

精彩评论

暂无评论...
验证码 换一张
取 消