0

We have a SQL Server 2012 Enterprise edition and an ASP.NET Web API 2.2 web service. The web API is the public interface for the database.

We need to end a production order doing:

  1. Changing a value in Production orders table.
  2. Generate a XML with all data related to this Production order.
  3. Save that XML file into server's file system.

I have thought to do it with a POST to Web API, but I don't know if I have to do the three previous steps ( Changing value, generating XML and saving it) in a Stored Procedure or maybe in the Web API Controller.

Maybe, if I do it with the API controller, it will take a long time and the client will time out.

1

Your three steps belong in the Model, not the Controller.

If it's a long-running operation, make it an asynchronous one. See Using Asynchronous Methods in ASP.NET MVC 4 and C#5, ASP.NET MVC 4, and asynchronous Web applications for more information.

1

If this process really does take a long time, you could also consider moving the long-running work out of process.

For example, when the request is handled by your Web API, you could write a message into something like a Message Queue and have MSMQ on the server activate a separate process to perform whatever work is indicated by that message.

How that work might be carried out is kind of orthogonal here - maybe a stored procedure works best in your case, or maybe a driver app to run business logic coded in a class library, etc. The point is that you can do this work without having to worry about it being in the context of a Web API thread - you can return immediately to the client and they don't have to wait for the long running process to complete, and you don't have to worry about timeouts on their connection, etc.

The caller can then poll for status update on the job, or you could use web sockets to push status back to the caller, or even use a separate Message Queue to write completion messages which the caller is monitoring, etc.

Of course this can only work if the caller is willing to accept a fully asynchronous workflow :)

  • Thanks for your answer. How can I implement a caller that accept a fully asynchronous workflow? – VansFannel Feb 6 '15 at 9:18
  • Well it kind of depends on what the caller is. Is this a web page that a person is invoking an action on? In that case you can make use of websockets (signalR) to push information back to the page with status information on the out-of-process job. But if it's an automated process that clls your WebAPI, you maybe can't do that so you might instead have it periodically poll the webapi for job completion status, etc. – Stephen Byrne Feb 6 '15 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.