您好,我在这两个对话框中遇到了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();
}
答案 0 :(得分:4)
在 Visual Studio 中,您可以检查call stack
,然后知道StackOverflowException
中确切的问题在哪里。
如果DialogA是DialogA_child
的基类,则在您的DialogA_child
的构造函数中,您的基类构造函数将递归调用它们自己。
因此您的调用堆栈应如下所示:
DialogA
构造函数添加新的DialogA_child
DialogA_child
呼叫基地(所以DialogA constructor
)DialogA
构造函数添加新的DialogA_child
DialogA_child
呼叫基地(所以DialogA constructor
)答案 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
,依此类推。
基本上,DialogA
和DialogA_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”。这将有助于两件事:
由于对话框必须使用唯一的ID,因此可以防止您意外地两次调用同一对话框。
跟踪更容易,而且您不必为它传递名称。例如,要调用对话框,您现在可以使用AddDialog(new DialogA());
代替AddDialog(new DialogA(DialogAId));
。
当前,您无法以想要的方式循环对话框(请参见下面的更新)。您不能:
DialogA
个电话DialogA_child
DialogA_child
再次致电DialogA
。如您所见,这会导致堆栈溢出。
相反,您可以间接调用它。
执行以下操作,而不是DialogA_child
调用DialogA
:
DialogA_child
的选择提示,并带有“重新启动对话框A”(或一些独特的选项)选项。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的要求将一个子对话框称为父对话框,但它仍然是新的,我还没有进行测试。