开发QnA Bot C#时出现错误CS4032

时间:2018-06-29 13:27:26

标签: c# azure chatbot

我正在尝试通过发送第一条消息“你好,你好吗?”来使我的机器人与用户打招呼。

这是我当前的代码:

 private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels

                ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));

                var reply = message.CreateReply();

                reply.Text = "Hello user how are you?";

                await client.Conversations.ReplyToActivityAsync(reply);
            }

这是我收到的错误:

Controllers\MessagesController.cs(55,17): error CS4032: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<Activity>'. [D:\home\site\wwwroot\Microsoft.Bot.Sample.QnABot.csproj]
Failed exitCode=1, command="D:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" "D:\home\site\wwwroot\.\Microsoft.Bot.Sample.QnABot.csproj" /nologo /verbosity:m /t:Build /p:AutoParameterizationWebConfigConnectionStrings=false;Configuration=Release;UseSharedCompilation=false /p:SolutionDir="D:\home\site\wwwroot\.\.\\"

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

HandleSystemMessage必须是异步的,返回类型必须是Task,以便在内部使用await。另外,您还需要更改HandleSystemMessage的代码调用。如果调用方法是异步的,则需要像await HandleSystemMessage一样调用。如果不是,则需要等待。