WCF Rest Asynchronous Calling Methods

2019-08-24 07:01发布

问题:

I have a class library I developed that is rather processing intensive that I currently call through a WCF REST service.

The REST service directly accesses the DLLs for the class library and more or less the WCF rest service is an interface for the system.

Let's say the following methods are defined:

Create Request

Starts a thread that takes five minutes, but immediately returns a session ID that the process generates and the thread uses to report when it is completed to the database.

Check Status

Accepts a session id and checks the database to see if the process has completed.

I have to think that there is a better way to "manage" the threads running, however, my requirements state that the user should receive an immediate response from the REST service upon issuing a request.

I am using the WCF Message property to return XML to the browser and as this application can be called from any programming language I can't use classic WCF and callbacks (I think, correct me if I am wrong).

Sometimes I run into an issue where an error occurs and the iscomplete event never gets written to the database and therefore the "Check Status" method says it's processing forever.

Does anyone have any ideas about what is normally done and what can be done in this situation?

Thanks!

Jeffrey Kevin Pry

回答1:

Your service should return a 202 Accepted at the initial request with a way for the client to check the current status, either through the Location header or as part of the content.

As you indicate the client then polls the URL indicated to check the current status. I would also suggest adding a bit of cache time to this response in case a client just starts looping.

How you handle things on the server is up to you and in no way related to REST. For one thing I would put all logic that executes as the background thread in a try/catch to you can return an error status back if an error occurs and possibly retry the action depending on the circumstances.



回答2:

I implemented a similiar process for importing/processing of large files and to be honest, I have never had a problem. Perhaps resolving the reason that the IsComplete never gets set will make this more resilient.

Not much of an answer, but still..