Botframework v4:从没有计算器异常的子对话框调用父对话框

据此我认为现在可以从子对话框调用父对话框。之前我不能这样做,因为它会导致堆栈溢出异常。我已经更新到 SDK 4.3 有谁知道如何实施这些更改吗?


主对话框调用对话框 A。


WaterfallStep[] waterfallSteps = new WaterfallStep[]

     {

        FirstStepAsync,

        SecondStepAsync,

        ThirdStepAsync,

     };

    AddDialog(new WaterfallDialog(InitialId, waterfallSteps));

    AddDialog(new DialogA(DialogAId));


  return await stepContext.BeginDialogAsync(DialogAId, cancellationToken: cancellationToken);

Dialog A 调用 Dialog Achild


    WaterfallStep[] waterfallSteps = new WaterfallStep[]

     {

        FirstStepAsync,

        SecondStepAsync,

        ThirdStepAsync,

     };

    AddDialog(new WaterfallDialog(InitialId, waterfallSteps));

    AddDialog(new DialogAchild(DialogAchildId));


   return await stepContext.BeginDialogAsync(DialogAchildId, cancellationToken: cancellationToken);

Dialog Achild 调用MainDialog,但这会产生Stack 溢出异常。


WaterfallStep[] waterfallSteps = new WaterfallStep[]

     {

        FirstStepAsync,

        SecondStepAsync,

        ThirdStepAsync,

     };

    AddDialog(new WaterfallDialog(InitialId, waterfallSteps));

    AddDialog(new MainDialog(MainDialogId));


  return await stepContext.BeginDialogAsync(MainDialogId, cancellationToken: cancellationToken);


函数式编程
浏览 64回答 1
1回答

茅侃侃

假设您在对话框的构造函数中添加对话框,那么您将产生一个无限循环,最终导致您所描述的堆栈溢出。MainDialog --> DialogA --> DialogAchild --> MainDialog --> infinite loop您提到的 PR 指的是一个稍微不同的问题。解决此问题的一种方法是从构造函数中删除导致最终循环的 AddDialog 方法。相反,将调用移至 AddDialogA() 之类的方法,并仅在需要时调用它。根据您的场景,您可以构建一个提供此类功能的基本对话框。这是您可以在必要时AuthenticatedDialog添加的产品示例。OnboardingDialog请注意,入职对话框本身继承自 AuthenticatedDialog,当您不卸载AddDialog()呼叫时,这也会导致无限循环。在基本对话框中抽象它很好,因为它为您提供了一些可以使用的 API。考虑将您的内容命名为 AddComponentDialog 或 UseComponentDialog。这样你就很好地表达了你的意图,潜在的读者一开始就知道你正在使用可重用的组件。AuthenticatedDialog.csusing Microsoft.Bot.Builder.Dialogs;using Microsoft.Bot.Schema;using Bot.Dialogs.Authentication;using Bot.Dialogs.Main;using Bot.Dialogs.Onboarding;using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Threading.Tasks;namespace Bot.Dialogs.Shared{&nbsp; &nbsp; public class AuthenticatedDialog : EnterpriseDialog&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private BotStateAccessors _accessors;&nbsp; &nbsp; &nbsp; &nbsp; private BotServices _botServices;&nbsp; &nbsp; &nbsp; &nbsp; public AuthenticatedDialog(BotServices botServices, string dialogId, BotStateAccessors accessors) : base(botServices, dialogId)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _accessors = accessors;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _botServices = botServices;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddDialog(new AuthenticationDialog("", accessors));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected async Task<DialogTurnResult> AskForOnboardingAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken, object stepResult = null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return await stepContext.BeginDialogAsync(nameof(OnboardingDialog), stepResult, cancellationToken);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected void AddOnboardingDialog()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddDialog(new OnboardingDialog(_botServices,_accessors));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}DialogA.cspublic class DialogA : AuthenticatedDialog{&nbsp; &nbsp; &nbsp; &nbsp; public DialogA(BotServices botServices, BotStateAccessors accessors) : base(botServices, nameof(DialogA), accessors)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var preferencesDispatchSteps = new WaterfallStep[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WaterfallStepA,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WaterfallStepB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddDialog(new FooDialog);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddOnboardingDialog();&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP