ASMX异步系统InvalidOperationException:当前无法启动异步操作。
创始人
2024-11-11 12:01:10
0

可以使用以下代码来解决这个问题:

[assembly: System.Web.UI.WebResource("MyNamespace.MyControl.js", "text/javascript")]

[ToolboxData("<{0}:MyControl runat=server>")] public class MyControl : WebControl { protected override void OnInit(EventArgs e) { base.OnInit(e);

    if (!this.DesignMode && this.Page != null && this.Page.Header != null)
    {
        string clientScript = @"function MyControl_OnCallbackComplete(result, context) 
                                { 
                                    // Callback code here
                                }";

        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(MyControl), "MyControlScript", clientScript, true);
    }
}

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public async Task GetSomeDataAsync(string param)
{
    var client = new HttpClient();
    HttpResponseMessage result = await client.GetAsync("http://example.com/api/data");
    var content = await result.Content.ReadAsStringAsync();

    return content;
}

}

这个问题通常是由于您尝试在未绑定Page的ASMX代​​码中尝试启动异步操作造成的。上面的代码示例演示了如何在OnInit事件中使用ScriptManager,以便正确地启动异步操作。

相关内容

热门资讯

玩家必备教程!wepoker游... 玩家必备教程!wepoker游戏安装教程(辅助挂)确实真的是有挂(有挂规律)1、首先打开wepoke...
实测必看!wepoker透视脚... 实测必看!wepoker透视脚本免费(辅助挂)总是真的有挂(有挂秘笈)1.wepoker透视脚本免费...
教程攻略!竞技联盟辅助插件(辅... 教程攻略!竞技联盟辅助插件(辅助挂)好像是有挂(果真有挂)1、下载好竞技联盟辅助插件脚本下载之后点击...
技术分享!xpoker透视辅助... 技术分享!xpoker透视辅助(辅助挂)真是有挂(有挂存在)一、xpoker透视辅助可以开透视的定义...
玩家必看科普!wejoker辅... 玩家必看科普!wejoker辅助软件(辅助挂)都是有挂(真实有挂)1、进入到wejoker辅助软件是...
一分钟了解!uupoker有透... 一分钟了解!uupoker有透视吗(辅助挂)真是有挂(有挂技术)1、进入游戏-大厅左侧-新手福利-激...
玩家必看分享!hhpoker真... 玩家必看分享!hhpoker真的假的(辅助挂)真是有挂(有挂助手)1、玩家可以在hhpoker真的假...
分享给玩家!wepoker透视... 分享给玩家!wepoker透视有用吗(辅助挂)本来是真的有挂(详细教程)1、wepoker透视有用吗...
总算了解!aapoker辅助工... 总算了解!aapoker辅助工具安全吗(辅助挂)其实存在有挂(确实有挂)1、超多福利:超高返利,海量...
发现玩家!德普之星辅助软件(辅... 发现玩家!德普之星辅助软件(辅助挂)竟然真的有挂(有挂方式)1、用户打开应用后不用登录就可以直接使用...