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

前端 未结 3 1841
再見小時候
再見小時候 2020-11-28 12:00

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

相关标签:
3条回答
  • 2020-11-28 12:39

    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.

    0 讨论(0)
  • 2020-11-28 12:43

    You might not be able to make the ASMX service itself behave in an async way, but if you are calling from a WebApp using javascript you can wrap a promise around the service when calling in order to take advantage of async within the client-side.

    As stated, it does not make your service async, but is an example of using this service in an async way within the client (ie, a javascript promise is used in this instance to halt further operation until the service has resolved. Similarly it can be adjusted to continue operation and perform other actions once resolved or failed).

    Optimising the service itself is only half of the solution, the other half is within the client, so I thought this would be a helpful expansion to some :)

    Javascript Code:

    var tripObj;
    
    async function getTripObject(tripId) {
    
    
      // Ensures Trip is fetched before proceeding 
      var promise = new Promise(function (resolve, reject) {
        ASMXService.getTrip(tripId, async function (ret) {
          tripObj = ret;
    
          resolve("Success");
        }, function () {
          reject("Error");
        });
      });
    
      await promise;
    
    }
    
    0 讨论(0)
  • 2020-11-28 12:44

    If your problem is simply integrating the top of your async logic in your asmx, you can do like the following snippet.

    [WebMethod(Description = "Takes an internal trip ID as parameter.")] 
    public Trip GetTrip(int tripid) {
       var trip = Trip.GetTrip(tripid).Wait();
       return trip; 
    }
    

    Be aware that if Trip.GetTrip() throws an exception, you will receive an AggregateException, and not the exception you would receive if it was await throwing the exception. You can do

    [WebMethod(Description = "Takes an internal trip ID as parameter.")] 
    public Trip GetTrip(int tripid) {
      try
      {
        var trip = Trip.GetTrip(tripid).Wait();
        return trip; 
      }
      catch(AggregateException ex)
      {
        throw ex.InnerException.First();
      }       
    }
    
    0 讨论(0)
提交回复
热议问题