本文介绍了为什么在StartAsync中使用context.Wait不能停止对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是一个愚蠢的问题,但是如果我不问的话,我会更愚蠢.我有以下代码

I'm sure is stupid question, but I will more stupid if I didn't askI have the following code

    [Serializable]
public class RootDialog : IDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("RootDialog !");

        context.Wait(MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        // calculate something for us to return
        int length = (activity.Text ?? string.Empty).Length;

        // return our reply to the user
        await context.PostAsync($"You sent {activity.Text} which was {length} characters");

        context.Wait(MessageReceivedAsync);
    }
}

我不太清楚语句上下文.等待用于等待用户的下一条消息.但是如果我启动RootDialog我的声明 等待context.PostAsync("RootDialog!");在我的陈述之后执行等待context.PostAsync($您发送的{activity.Text}是{length}个字符");也被驱逐了.

I understant the statement context.Wait is use to wait the next message of the user.But if I launch my RootDialog my statement await context.PostAsync("RootDialog !");Is executed and just after my statement await context.PostAsync($"You sent {activity.Text} which was {length} characters");is exectuted too.

为什么?

为什么我没有在语句中暂停一下语句 context.Wait(MessageReceivedAsync);在StartAsync函数中,就像在MessageReceivedAsync函数中一样?

Why I didn't a pause in my programm with the statement context.Wait(MessageReceivedAsync);in the StartAsync function like I have in the MessageReceivedAsync function ?

推荐答案

在此处的文档中有IDialogContext.Wait的一些描述: https://docs.microsoft.com/zh-cn/bot-framework/dotnet/bot-builder-dotnet- dialogs#implementation-details

There is some description of IDialogContext.Wait in the docs here: https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-dialogs#implementation-details

在服务器本身不跟踪或存储会话信息的意义上,使用BotBuilder SDK构建的机器人是宁静且无状态的. "context.Wait(method)"并不意味着在此处冻结代码",而是:下次用户收到消息时,在对话框中的此方法中恢复.下一步调用的方法实际上是通过对话框序列化的,并存储在State Service中(请参见此处:管理状态数据最后一个上下文.下次用户在同一会话的上下文中发送消息时,将调用等待(方法名称).

A bot built using the BotBuilder SDK is restful and stateless, in the sense that the server itself doesn't track or store session information. "context.Wait(method)" doesn't mean "freeze the code here", but rather: resume at this method in the dialog the next time a message comes from the user. The method to call next is actually serialized with the dialog and stored in the State Service (see here: Manage state data The last context.Wait(methodname) will be called the next time the user sends a message in the context of the same conversation.

一个示例可能会有用:

[Serializable]
public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        // calculate something for us to return
        int length = (activity.Text ?? string.Empty).Length;

        // return our reply to the user
        await context.PostAsync($"You sent {activity.Text} which was {length} characters");


        context.Wait(MessageReceivedAsync2);
    }

    private async Task MessageReceivedAsync2(IDialogContext context, IAwaitable<object> result)
    {
        await context.PostAsync($"Second MessageReceived");

        context.Wait(MessageReceivedAsync);
    }
}

上面的代码将针对用户发送的每条消息在MessageReceivedAsync和MessageReceivedAsync2之间来回切换.

The code above will switch back and forth between MessageReceivedAsync and MessageReceivedAsync2 for every message sent by the user.

这篇关于为什么在StartAsync中使用context.Wait不能停止对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:13