有什么方法可以处理 ASMX 服务背后的异步/等待吗? [英] Is there some way to handle async/await behind an ASMX service?

查看:40
本文介绍了有什么方法可以处理 ASMX 服务背后的异步/等待吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,它为 JSON 和 ASMX Web 服务提供 WCF REST API.该应用程序已经存在了几年.它基于 ASP.NET 2.0,但几年前升级到 .NET 4.0,我刚刚升级到 .NET 4.5 以便能够使用新的异步框架.

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.

应用程序背后是一些遗留服务,我意识到通过异步来提高性能有很大的潜力.我已经在整个应用程序中实现了异步,并且一切都通过 WCF REST API 完美运行.

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.

太晚了,我发现 ASMX 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;
}

然后我了解到 ASMX 根本不支持 async/await,每个人都建议迁移到 WCF.我对此并不太高兴.ASMX(实际上是其中三个)充满了不同的方法,我们希望继续从旧 API 提供大量 API 使用者.

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.

但我们需要提高性能!有没有人知道一种解决方法,这样我就可以继续在 ASMX 后面使用 async/await,但像以前一样公开 ASMX?

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?

推荐答案

这样做或许可行,但会有点别扭.ASMX 支持 APM 风格的异步方法,您可以将 TAP 转换为 APM(但是,请注意该页面上的 MSDN 示例没有正确传播异常).

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).

我的博客上有一个示例,显示了如何在 APM 中包装 TAP 实现(具有保持正确异常类型但丢失堆栈的异常传播;请参阅 ExceptionDispatchInfo 用于完全正确的异常传播).当 WCF 仅支持 APM 时,我使用了一段时间.一个非常相似的方法应该适用于 ASMX.

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.

但是,请注意,您将 必须以 4.5(即 httpRuntime.targetFramework)为目标,async/await 才能按预期工作.

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

这篇关于有什么方法可以处理 ASMX 服务背后的异步/等待吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆