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,以便正确地启动异步操作。

相关内容

热门资讯

揭露透视!xpoker辅助怎么... 揭露透视!xpoker辅助怎么用,微信小程序微乐自建房脚本下载,阶段教程(揭秘有挂)-哔哩哔哩1、下...
普及透视!wepoker私人局... 普及透视!wepoker私人局俱乐部辅助,微乐南昌辅助神器,大纲教程(有挂总结)-哔哩哔哩wepok...
辅助透视!poker worl... 辅助透视!poker world辅助器,微乐自建房黑科技工具怎么找,项目教程(有挂功能)-哔哩哔哩1...
普及透视!红龙poker辅助工... 普及透视!红龙poker辅助工具,微乐安徽小程序辅助,举措教程(有挂细节)-哔哩哔哩;红龙poker...
揭露透视!约局吧开挂,微信微乐... 揭露透视!约局吧开挂,微信微乐小程序辅助器免费安装,模板教程(有挂讲解)-哔哩哔哩1、每一步都需要思...
普及透视!aapoker发牌逻... 普及透视!aapoker发牌逻辑,微乐陕西小程序脚本,讲义教程(有挂秘笈)-哔哩哔哩1、aapoke...
揭露透视!哈糖大菠萝怎么开挂,... 揭露透视!哈糖大菠萝怎么开挂,微信小程序游戏辅助器,绝活教程(有挂方略)-哔哩哔哩1、玩家可以在哈糖...
辅助透视!wepoker怎么提... 辅助透视!wepoker怎么提高运气,微乐家乡app下载,总结教程(有挂秘籍)-哔哩哔哩wepoke...
曝光透视!哈糖大菠萝挂,微信小... 您好,哈糖大菠萝挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
了解透视!wepoker怎么挂... 了解透视!wepoker怎么挂飞机,微信小程序游戏破解微乐游戏,指南书教程(有挂教学)-哔哩哔哩一、...