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

相关内容

热门资讯

透视教程"aapok... 透视教程"aapoker免费透视脚本"详细辅助安装教程(透视脚本视频辅助);支持2-10人实时对战,...
透视苹果版"wepo... 1、透视苹果版"wepoker手机插件"详细辅助2025新版技巧(可以获得好牌攻略)(UU poke...
透视脚本"wepok... 1、透视脚本"wepoker软件靠谱么"详细辅助玩家教你(私人局辅助透明)。2、wepoker软件靠...
透视神器"wepok... 透视神器"wepoker插件辅助"详细辅助透牌教程(数据分析工具方法);最新版2024是一款经典耐玩...
辅助透视"hhpok... 辅助透视"hhpoker外挂靠谱吗"详细辅助实用技巧(辅助技巧)是由北京得hhpoker外挂靠谱吗黑...
透视安装"wepok... 透视安装"wepoker模拟器哪个"详细辅助靠谱教程(私人局开挂视频技巧)准备好在wepoker模拟...
透视挂透视"wepo... 1、透视挂透视"wepoker有没有辅助"详细辅助我来教教你(可以获得好牌透视)。2、wepoker...
透视规律"aapok... 透视规律"aapoker透视脚本入口"详细辅助力荐教程(科技辅助器教程);1分钟了解详细教程(微信 ...
透视规律"来玩app... 1、透视规律"来玩app破解"详细辅助2025新版总结(辅助软件技巧)。2、来玩app破解透视辅助简...
透视有挂"wepok... 透视有挂"wepoker辅助透视"详细辅助曝光教程(可以开辅助技巧);最新版2024是一款经典耐玩的...