简单对话框中的Stackoverflow异常

时间:2019-02-26 12:42:45

标签: c# botframework stack-overflow

您好,我在这两个对话框中遇到了Stackoverflow异常。从主对话框类中调用Dialog A。对话框A可以选择转到Dialog A child,而Dialog A child可以选择返回Dialog A。但是它正在收到一个Stackoverflow异常。当我从另一个删除一个:从Dialog A child删除Dialog A或从Dialog A删除Dialog A child的示例时,异常错误消失了。简而言之,当两个对话框可以相互调用时,它将引发Stackoverflow异常。

我知道在这种特定情况下,我只能EndDialogAsync回到Dialog A,但我真正的对话流程并不像这样 。该如何解决?

对话框A代码:

 public class DialogA : ComponentDialog
{
    private const string InitialId = "dialogA";
    private const string ChoicePrompt = "choicePrompt";
    private const string DialogAchildId = "dialogA_childId";

    public DialogA(string dialogId)
        : base(dialogId)
    {
        InitialDialogId = InitialId;

        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
            FirstStepAsync,
            SecondStepAsync,
            ThirdStepAsync,
         };
        AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
        AddDialog(new ChoicePrompt(ChoicePrompt));
        AddDialog(new DialogA_child(DialogAchildId));

    }

    private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.PromptAsync(
            ChoicePrompt,
            new PromptOptions
            {
                Prompt = MessageFactory.Text($"Here are your choices:"),
                Choices = new List<Choice>{new Choice { Value = "Open Dialog A_Child", }, new Choice { Value = "Open Dialog B_Child" }, },
                RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
            });
    }

    private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        var response = (stepContext.Result as FoundChoice)?.Value.ToLower();

        if (response == "open dialog a_child")
        {
            return await stepContext.BeginDialogAsync(DialogAchildId, cancellationToken: cancellationToken);
        }

        return await stepContext.NextAsync();
    }

    private static async Task<DialogTurnResult> ThirdStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.EndDialogAsync();
    }

对话框子代码:

 public class DialogA_child : ComponentDialog
{
  private const string InitialId = "dialogAchild";
    private const string ChoicePrompt = "choicePrompt";
    private const string DialogAId = "dialogAId";

    public DialogA_child(string dialogId)
        : base(dialogId)
    {
        InitialDialogId = InitialId;

        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
            FirstStepAsync,
            SecondStepAsync,
         };
        AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
        AddDialog(new DialogA(DialogAId));

    }

    private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.PromptAsync(
       ChoicePrompt,
       new PromptOptions
       {
           Prompt = MessageFactory.Text($"Here are your choices:"),
           Choices = new List<Choice> {new Choice { Value = "Open Dialog A" }, new Choice { Value = "Open Dialog B" }, },
           RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
       });
    }

    private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        var response = (stepContext.Result as FoundChoice)?.Value.ToLower();

        if (response == "open dialog a")
        {
            return await stepContext.BeginDialogAsync(DialogAId, cancellationToken: cancellationToken);
        }

        return await stepContext.NextAsync();
    }

调用对话框A的主要代码:

   public class MainDialog : ComponentDialog
{
    private const string InitialId = "mainDialog";
    private const string ChoicePrompt = "choicePrompt";
    private const string DialogAId = "dialogAId";

    public MainDialog(string dialogId)
        : base(dialogId)
    {
        InitialDialogId = InitialId;

        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
            FirstStepAsync,
            SecondStepAsync,
            ThirdStepAsync,
         };
        AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
        AddDialog(new ChoicePrompt(ChoicePrompt));
        AddDialog(new DialogA(DialogAId));
    }

    private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.PromptAsync(
            ChoicePrompt,
            new PromptOptions
            {
                Prompt = MessageFactory.Text($"Here are your choices:"),
                Choices = new List<Choice>{ new Choice { Value = "Open Dialog A" }, new Choice { Value = "Open Dialog B" }, },
                RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
            });
    }

    private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        var response = (stepContext.Result as FoundChoice)?.Value.ToLower();

        if (response == "open dialog a")
        {
            return await stepContext.BeginDialogAsync(DialogAId, cancellationToken: cancellationToken);
        }

        if (response == "open dialog b")
        {
        }

        return await stepContext.NextAsync();
    }

    private static async Task<DialogTurnResult> ThirdStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {

        return await stepContext.EndDialogAsync();
    }

2 个答案:

答案 0 :(得分:4)

Visual Studio 中,您可以检查call stack,然后知道StackOverflowException中确切的问题在哪里。

如果DialogA是DialogA_child的基类,则在您的DialogA_child的构造函数中,您的基类构造函数将递归调用它们自己。

因此您的调用堆栈应如下所示:

  1. DialogA构造函数添加新的DialogA_child
  2. DialogA_child呼叫基地(所以DialogA constructor
  3. DialogA构造函数添加新的DialogA_child
  4. DialogA_child呼叫基地(所以DialogA constructor
  5. ...

答案 1 :(得分:1)

@koviroli的答案是100%正确的,因此,一旦您感到理解,请接受他的答案。我将其添加为其他答案,因为您似乎在努力理解一些东西,而评论限制了我提供良好的解释。

构造函数的简要说明

由于您是C#的新手,所以我将简要介绍构造函数。 DialogA_child的构造函数是这一部分:

public DialogA_child(string dialogId)
    : base(dialogId)
{
    InitialDialogId = InitialId;

    WaterfallStep[] waterfallSteps = new WaterfallStep[]
     {
        FirstStepAsync,
        SecondStepAsync,
     };
    AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
    AddDialog(new DialogA(DialogAId));

}

每当您使用new DialogA_child("xyz")时,都会调用该构造函数来“构造” DialogA_child:base(dialogId)使得“ xyz”被发送到DialogA_child的基类(即ComponentDialog)的构造函数中。在ComponentDialog的构造函数中,它将传入的参数(在本例中为“ xyz”)设置为dialogId。

如果您在代码中单击ComponentDialog并按F12,它将带您到ComponentDialog的定义,您将看到以下内容:

public ComponentDialog(string dialogId);

这是怎么了

DialogA_child的构造函数中,您拥有:AddDialog(new DialogA(DialogAId));,它创建DialogA new 实例。然后,在DialogA的构造函数中,您有AddDialog(new DialogA_child(DialogAchildId));,它创建了另一个 DialogA_child,依此类推。

基本上,DialogADialogA_child继续创建彼此的新实例,从而导致StackOverflow。

最简单的解决方法是仅删除AddDialog(new DialogA(DialogAId));

附加说明

同样,我知道您是C#的新手,所以我将为您提供其他帮助。

private const string ChoicePrompt = "choicePrompt";

应该是

private const string ChoicePromptId = "choicePrompt";

因为ChoicePrompt已被定义为一种提示类型。

在定义对话框构造函数时,最简单的方法是:

public DialogA() : base(nameof(DialogA))

这将自动将DialogA的ID设置为“ DialogA”。这将有助于两件事:

  1. 由于对话框必须使用唯一的ID,因此可以防止您意外地两次调用同一对话框。

  2. 跟踪更容易,而且您不必为它传递名称。例如,要调用对话框,您现在可以使用AddDialog(new DialogA());代替AddDialog(new DialogA(DialogAId));

强制对话框循环

当前,您无法以想要的方式循环对话框(请参见下面的更新)。您不能:

  1. DialogA个电话DialogA_child
  2. 然后让DialogA_child再次致电DialogA

如您所见,这会导致堆栈溢出。

相反,您可以间接调用它。

执行以下操作,而不是DialogA_child调用DialogA

  1. 具有DialogA_child的选择提示,并带有“重新启动对话框A”(或一些独特的选项)选项。
  2. OnTurnAsync(在bot的主Class文件中)中,检查用户是否响应了“重新启动对话框A”。如果是这样,请清除所有对话框(或仅替换对话框),然后再次开始DialogA

代码:

DialogA_child

private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
    return await stepContext.PromptAsync(
    choicePrompt,
    new PromptOptions
    {
        Prompt = MessageFactory.Text($"Here are your choices:"),
        Choices = new List<Choice> { new Choice { Value = "Restart Dialog A" }, new Choice { Value = "Open Dialog B" }, },
        RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
    });
}

<myBot>.cs

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    var activity = turnContext.Activity;

    var dc = await Dialogs.CreateContextAsync(turnContext);

    if (activity.Text == "Restart Dialog A")
    {
        await dc.CancelAllDialogsAsync();
        await dc.BeginDialogAsync(nameof(DialogA));
    }

更新

BotBuilder SDK V4.3即将发布,它允许任何子对话框或同级对话框调用父级定义的任何对话框。参见this pull request。我相信,您可以按照OP的要求将一个子对话框称为父对话框,但它仍然是新的,我还没有进行测试。