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

相关内容

热门资讯

透视揭露!有哪些免费的wpk透... 透视揭露!有哪些免费的wpk透视码(透视)aapoker透视怎么用,教程诀窍(真是有挂)-哔哩哔哩运...
大菠萝789辅助器下载!we ... 大菠萝789辅助器下载!we poker辅助器(透视)工具-竟然必备存在有挂1、许多玩家不知道大菠萝...
透视曝光!pokemmo手机版... 透视曝光!pokemmo手机版脚本(透视)hhpoker透视方法,教程讲义(有挂分析)-哔哩哔哩1、...
德普之星有透视辅助吗!poke... 德普之星有透视辅助吗!pokemmo脚本手机版(透视)挂-其实辅助存在有挂德普之星有透视辅助吗破解侠...
透视解谜!hhpoker德州机... 透视解谜!hhpoker德州机器人(透视)德州透视插件,教程手筋(有挂规律)-哔哩哔哩1、完成hhp...
德州局脚本!hhpoker德州... 德州局脚本!hhpoker德州透视(透视)技巧-真是透视存在有挂1、hhpoker德州透视公共底牌简...
透视解谜!sohoo辅助(透视... 透视解谜!sohoo辅助(透视)hhpoker怎么开透视,教程手段(证实有挂)-哔哩哔哩1、hhpo...
wepoker究竟有没有透视!... wepoker究竟有没有透视!wepoker有辅助工具吗(透视)攻略-原来开挂真的有挂1、金币登录送...
透视透视!聚星ai辅助工具收费... 透视透视!聚星ai辅助工具收费多少(透视)aapoker插件,教程指南(有挂教程)-哔哩哔哩聚星ai...
wepoker私人局怎么玩!德... 您好,wepoker私人局怎么玩这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...