动态 FormFlow 中确认提示时出现 KeyNotFoundException

KeyNotFoundException on confirm prompt in dynamic FormFlow

SDK版本:3.16.1.38846 代码:.Net

问题描述

我们正在使用 Field.SetDefine 根据调用服务获得的值实时定义字段按钮。具体来说,我们有一个 Source 字段和一个 Destination 字段,Destination 和 Source 字段的按钮是根据服务 当用户完成表单,但在确认提示中单击 时,他会收到以下异常:

未能完成表格。异常:字典中不存在给定的键。

   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Field`1.ValueDescription(Object value)
   at Microsoft.Bot.Builder.FormFlow.Advanced.RecognizeEnumeration`1.<>c__DisplayClass2_0.<.ctor>b__0(Object value)
   at Microsoft.Bot.Builder.FormFlow.Advanced.RecognizeEnumeration`1.ValueDescription(Object value)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.ValueDescription(IField`1 field, Object value, String format)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.ExpandTemplate(String template, String currentChoice, String noValue, T state, IField`1 field, Object[] args, IList`1& buttons)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.Prompt(T state, IField`1 field, Object[] args)
   at Microsoft.Bot.Builder.FormFlow.Advanced.NavigationField`1..ctor(String name, String startField, IForm`1 form, T state, FormState formState, Fields`1 fields)
   at Microsoft.Bot.Builder.FormFlow.Advanced.NavigationStep`1..ctor(String startField, IForm`1 form, T state, FormState formState)
   at Microsoft.Bot.Builder.FormFlow.FormDialog`1.<MessageReceived>d__16.MoveNext()
   --- End of inner exception stack trace ---

我们认为这可能是因为 FormFlow 代码试图访问该字段的值,但按钮列表已更改,现在该值不再存在于列表中。

如何避免这个异常。

示例代码

重现问题的完整机器人程序代码。

public class DynaForm
{
    public static ICollection<string> CustomerAccounts { get; set; }
    public string Destination { get; set; }
    public string source { get; set; }


    public static IForm<DynaForm> BuildFormAsync()
    {
        return new FormBuilder<DynaForm>()
            .Message("Welcome to the simple sandwich order bot!")
            .Field(new FieldReflector<DynaForm>(nameof(source))
                .SetType(null)
                .SetDefine((state, field) =>
                {
                foreach (var item in CustomerAccounts)
               {
                   field
                       .AddDescription(item,item)
                       .AddTerms(item, item);
               }
               return Task.FromResult(true);                   
                })             
                .SetPrompt(new PromptAttribute("Select the source account \n {||} \n") { ChoiceStyle = ChoiceStyleOptions.Buttons })
                .SetAllowsMultiple(false)
                )                  
            .Field(new FieldReflector<DynaForm>(nameof(Destination))
                .SetType(null)
                .SetDependencies(nameof(source))
                .SetDefine((state, field) =>
                {
                foreach (var item in CustomerAccounts)
                {
                  field
                    .AddDescription(item, item)
                    .AddTerms(item, item);
                }
                return Task.FromResult(true);
                })
                .SetPrompt(new PromptAttribute("Select the destination account \n {||} \n") { ChoiceStyle = ChoiceStyleOptions.Buttons })                 
                .SetAllowsMultiple(false)                 
                )
            .Confirm("Do you want to continue? {||}")           
            .Build();   
    }
}

预期行为

当用户 select 拒绝确认提示时,应显示表单字段以供用户 select 更改字段。

enter image description here

我通过 GitHub 回复了您,因为您似乎将同一个问题发布了两次:https://github.com/Microsoft/BotBuilder/issues/5060