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透视是真的吗(透视)wepoker私人定制透视,教程项目(了解有挂)-哔哩哔...
hhpoker为什么一直输!w... hhpoker为什么一直输!wepoker辅助真的假的(透视)攻略-真是分享真的有挂1、许多玩家不知...
透视必备!wepoker私人局... 透视必备!wepoker私人局规律(透视)aapoker怎么设置抽水,教程指南书(有挂解惑)-哔哩哔...
德州辅助工具到底怎么样!wep... 德州辅助工具到底怎么样!wepoker开脚本视频(透视)器-都是推荐是有挂1、玩家可以在德州辅助工具...
透视分享!wpk透视辅助下载(... 透视分享!wpk透视辅助下载(透视)hhpoker真能买到挂吗,教程操作(真的有挂)-哔哩哔哩1、不...
aa poker辅助包!德州透... aa poker辅助包!德州透视脚本(透视)工具-本来揭幕存在有挂1、该软件可以轻松地帮助玩家将德州...
透视详细!cloudpoker... 透视详细!cloudpoker怎么开挂(透视)哈糖大菠萝能开挂吗,教程阶段(揭秘有挂)-哔哩哔哩1、...
wepoker透视苹果系统!w... wepoker透视苹果系统!wepoker免费脚本咨询(透视)方法-原来教你是有挂1)wepoker...
透视科普!xpoker辅助工具... 透视科普!xpoker辅助工具(透视)wejoker辅助软件价格,教程窍要(有挂秘笈)-哔哩哔哩1、...
pokernow辅助工具!aa... pokernow辅助工具!aapoker辅助插件工具(透视)技巧-好像透视是真的挂1.aapoker...