r3plica
r3plica

Reputation: 13387

Microsoft Bot Framework working with database results

I am trying to get my Dialog to work with my database. If I have my dialog like this:

[Serializable]
public class QuestionDialog : IDialog<object>
{

    /// <summary>
    /// Start our response
    /// </summary>
    /// <param name="context">The current context</param>
    /// <returns></returns>
    public async Task StartAsync(IDialogContext context)
    {

        // Move to the next method
        context.Wait(StepOneAsync);
    }

    /// <summary>
    /// When our message is recieved we execute this delegate
    /// </summary>
    /// <param name="context">The current context</param>
    /// <param name="result">The result object</param>
    /// <returns></returns>
    private async Task StepOneAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {

        // Get our activity
        var activity = await result;

        // Ask our first question
        await context.PostAsync("hi");

        // Get our answer
        context.Done(this);
    }
}

Everything works fine and I get my message as expected. I then changed it to this:

[Serializable]
public class QuestionDialog : IDialog<object>
{

    // Private properties
    private IList<QuestionGroup> _questionGroups;

    /// <summary>
    /// Start our response
    /// </summary>
    /// <param name="context">The current context</param>
    /// <returns></returns>
    public async Task StartAsync(IDialogContext context)
    {

        try

        {

            // Create our service
            var questionGroupService = new QuestionGroupService(new UnitOfWork<DatabaseContext>());

            // Add our question groups
            this._questionGroups = await questionGroupService.ListAllAsync();

            // Move to the next method
            context.Wait(StepOneAsync);
        } catch (Exception ex)
        {

        }
    }

    /// <summary>
    /// When our message is recieved we execute this delegate
    /// </summary>
    /// <param name="context">The current context</param>
    /// <param name="result">The result object</param>
    /// <returns></returns>
    private async Task StepOneAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {

        // Get our activity
        var activity = await result;

        // Ask our first question
        await context.PostAsync("hi");

        // Get our answer
        context.Done(this);
    }
}

And it doesn't go the the StepOneAsync method. Can anyone see anything glaringly obvious as to why this isn't working?

Upvotes: 1

Views: 124

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

Make sure your QestionGroup model is marked as Serializable.

If you cannot make it serializable and you still want to reference it during your dialog, you need to go with one of the alternatives described in the "How can I reference non-serializable services from my C# dialogs?" section of the Bot Framework Technical FAQ.

The simplest one is to use the NonSerialized attribute in your field.

Alternatively, you can try using registering the Reflection Serialization Surrogate by adding it to the Autofac container. In your global.asax, try adding this code:

var builder = new ContainerBuilder();

builder.RegisterModule(new ReflectionSurrogateModule());

builder.Update(Conversation.Container);

Upvotes: 1

Related Questions