Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way to handle async/await behind an ASMX service?

Tags:

I have a web app serving a WCF REST API for JSON and an ASMX web service. The application has been around for a few years. It's based on ASP.NET 2.0, but upgraded to .NET 4.0 a couple of years ago, and I just upgraded to .NET 4.5 to be able to use the new async framework.

Behind the application are some legacy services, and I realized that there is a big potential for increasing the performance by going async. I have implemented async all the way through the application, and everything is working perfectly through the WCF REST API.

Too late I discovered that the ASMX API fails, I wanted methods like this:

[WebMethod(Description = "Takes an internal trip ID as parameter.")] async public Task<Trip> GetTrip(int tripid) {     var t = await Trip.GetTrip(tripid);     return t; } 

I then learned that async/await isn't supported in ASMX at all, and everybody advises to migrate to WCF. I am not too joyful about this. The ASMX (actually three of them) are stuffed with different methods, and there are loads of API consumers that we want to keep serving from the old API.

But we need the increased performance! Does anybody know about a workaround so I can keep using async/await behind the ASMX, but expose the ASMX as before?

like image 755
Barslett Avatar asked Sep 04 '13 19:09

Barslett


People also ask

Does async await block main thread C#?

The await operator doesn't block the thread that evaluates the async method. When the await operator suspends the enclosing async method, the control returns to the caller of the method.

Does async await run on different threads?

Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.

Can async method have multiple awaits?

For more information, I have an async / await intro on my blog. So additionally, if a method with multiple awaits is called by a caller, the responsibility for finishing every statement of that method is with the caller.

Is it mandatory to use await with async?

await waits for async to resolve or reject. if you dont need the results, you wont need to await. If you don't await it (or otherwise handle the error, e.g. with . catch ), and it errors, you have an "unhandled promise rejection".


1 Answers

It may be possible to do this, but it would be a bit awkward. ASMX supports APM-style asynchronous methods, and you can convert TAP to APM (however, note that the MSDN example on that page does not propagate exceptions correctly).

I have an example on my blog that shows how to wrap TAP implementations in APM (with exception propagation that keeps the correct exception type but loses the stack; see ExceptionDispatchInfo for fully correct exception propagation). I used this for a while when WCF only supported APM. A very similar approach should work for ASMX.

However, note that you will have to target 4.5 (i.e., httpRuntime.targetFramework) for async/await to work as expected.

like image 87
Stephen Cleary Avatar answered Sep 23 '22 12:09

Stephen Cleary