web client call server, server call 3rd party, all async?

840 views Asked by At

I have a website that is that is architected using an n-layer approach. The problem I have is that I need the client to make a call to the application layer, one of the other layers will then make a call to another web service somewhere in the world. The other web service could take some time to come back so what I would like to do, if possible, something along the lines of the following using async requests: simple diagram of the async requests

The client is HTML & JavaScript, the server layers are written in C# (.NET 4.5), the 3rd party web services are just web services I need to consume

How would you go about writing this? Any help will be much appreciated

2

There are 2 answers

1
David On

It doesn't look like the server-side code needs to call the external service asynchronously. I'm not seeing an advantage in doing that.

The client-side code would call the server-side code asynchronously, of course. (An AJAX request, using jQuery for example.) And it would await the response form that service in an asynchronous manner before handling that response. But since the server-side code in this request is only serving that one request, it can do so synchronously.

Indeed, if the server-side code were itself also asynchronous then it would return control to the client-side code immediately before it has anything useful to give it. Which means the client-side asynchronous handler can't do anything. Instead, when the server-side code gets a response from the external service, it would need to push that response to the client-side code. Which is possible with websockets and whatnot, but probably a lot more complex than this situation requires.

Only the first link in the chain needs to be asynchronous in order to provide the user experience of asynchronicity, the rest of the system doesn't need to be.

4
Stephen Cleary On

Whenever you have a "client/server" boundary, you have the option of making the server asynchronous as well as the option of making the client asynchronous. Those decisions are independent.

I'll make the assumption that your third-party webservice is scalable, or that your server has other things to do besides just this one request. In that case, I'd recommend that your server be asynchronous.

Asynchronous programming is natural with async/await in ASP.NET 4.5. Depending on your third-party service, you may want to use HttpClient or the async-compatible WCF or webservice proxies. Both ASP.NET MVC and WebAPI support asynchronous calls for your service.

On the client side, you have no choice; JavaScript in the browser must be asynchronous.