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

时间:2018-09-18 09:53:48

标签: c# azure botframework bots 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代码正在尝试访问该字段的值,但是按钮列表已更改,并且此值现在不再存在于列表中。

如何避免这种例外情况。

示例代码

重现问题的完整bot代码。

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();   
    }
}

预期行为

当用户在确认提示中选择“否”时,将显示表单字段供用户选择要更改的字段。

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过GitHub回复了您,因为看起来您有两次发同样的问题:https://github.com/Microsoft/BotBuilder/issues/5060