Azure Bot Framework LUIS / FormFlow Dialog Error: Index was outside the bounds of the array

I have been building a chat bot using the Azure Bot Framework, and I came across an error that is pretty common when using new frameworks. That is to say, what should have worked fine, wasn’t working as expected!

Essentially what I am doing is displaying a LUIS dialog, and then based on the response from the user, I am then displaying a FormFlow dialog where the user is stepped through some basic questions to populate a JobEnquiry model.

When the user has answered all the questions, I call back into JobEnquiryFormComplete and await the result, which should be a JobEnquiry. However I was getting an Index was outside the bounds of the array error on the var formState = await result; line.

[Serializable]
public class JobEnquiry
{
    public JobEnquiry()
    {
        Hours = 12;
    }
    public string Industry { get; set; }
    public string Interests { get; set; }
    public string CustomerName { get; set; }
    [Prompt(AllowDefault = BoolDefault.True)]
    public DateTime Date { get; set; }
    [Prompt(AllowDefault = BoolDefault.True)]
    public double Hours { get; set; }
    public static IForm<JobEnquiry> BuildForm()
    {
        return new FormBuilder<JobEnquiry>()
                .Message("Welcome to the job enquiry form.")
                .Build();
    }
}

[LuisIntent("ShowJobs")]
public async Task ShowJobs(IDialogContext context, LuisResult result)
{
    context.Call(FormDialog.FromForm(JobEnquiry.BuildForm, FormOptions.PromptInStart), JobEnquiryFormComplete);
}

private async Task JobEnquiryFormComplete(IDialogContext context, IAwaitable<JobEnquiry> result)
{
    var formState = await result;
    string reply = "Job enquiry form complete. Data:\n\n" + JsonConvert.SerializeObject(formState);
    await context.PostAsync(reply);
    context.Wait(this.MessageReceived);
}

The cause of this error is the [Prompt(AllowDefault = BoolDefault.True)] attribute. This leaves the Date and Hours field values empty, and when the IAwaitable is converted into JSON, the Index was outside the bounds of the array error is thrown.

In this case, the solution is to simply remove the [Prompt(AllowDefault = BoolDefault.True)] attribute.

Alternatively, I could also resolve it by ensuring that the JobEnquiry model is pre-populated (within a constructor) to ensure that there are no empty/null values.

 

Posted on Jan 31, 2017 in Articles | Tags: .NETAzureBot FrameworkAI