C#目前无法启动异步操作. [英] C# An asynchronous operation cannot be started at this time.

查看:70
本文介绍了C#目前无法启动异步操作.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行我的代码,因为我单击了以ASP Web表单提交按钮.单击按钮后立即发生错误有人知道原因吗?错误消息显示如下:

I am trying to run my code as I click the button submit in asp web form. The error occurred as soon as I click the button Anyone knows the reason? the error message shows like this:

"/"应用程序中的服务器错误.

Server Error in '/' Application.

此时无法启动异步操作.异步操作只能在异步处理程序中启动,或者模块或在页面生命周期中的某些事件期间.如果这执行页面时发生异常,请确保该页面是标记为<%@页面Async ="true"%>.

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

aspx文件:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
    <h1>Luis</h1>        
        <asp:TextBox ID="TextBox1" runat="server" Height="40px" Width="273px"></asp:TextBox>    
</div>
    <p>
       <input id="Submit1"  runat="server" type="submit" value="Check" onserverclick="Submit_Click"/></p>  
    <p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</asp:Content>

.cs文件:

public partial class _Default : Page
{
public async void Submit1_Click(object sender, EventArgs e) {

    string utterance = TextBox1.Text;

    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // This app ID is for a public sample app that recognizes requests to turn on and turn off lights
    var luisAppId = "75bcaaff-1dc1-4dsa-adf7-63584cea339a";
    var subscriptionKey = "43314f19c5ecgdascba6a00f1d3cc3533";

    // The request header contains your subscription key
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

    // The "q" parameter contains the utterance to send to LUIS
    //queryString["q"] = "turn on the left light";

    // These optional request parameters are set to their default values
    queryString["timezoneOffset"] = "0";
    queryString["verbose"] = "false";
    queryString["spellCheck"] = "false";
    queryString["staging"] = "false";

    var uri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString + "&q=" + utterance;
    var response = await client.GetAsync(uri);

    var strResponseContent = await response.Content.ReadAsStringAsync();

    // Display the JSON result from LUIS
    //Console.WriteLine(strResponseContent.ToString());
    Label1.Text = strResponseContent.ToString();
}
}

推荐答案

您正在调用 ASYNC 方法,答案出在错误中.

You are calling an ASYNC method and the answer is in the error.

确保该页面被标记为<%@页面Async ="true"%>.

ensure that the Page is marked <%@ Page Async="true" %>.

这篇关于C#目前无法启动异步操作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆