Microsoft Bot Framework使用数据库结果

时间:2016-11-15 15:14:06

标签: c# botframework

我想让我的Dialog与我的数据库一起工作。 如果我有这样的对话:

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

一切正常,我按预期得到了我的信息。然后我把它改成了这个:

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

它不是 StepOneAsync 方法。任何人都可以看到明显的原因,为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

确保您的QestionGroup模型标记为Serializable。

如果您无法将其序列化并且仍希望在对话期间引用它,则需要使用&#34; How can I reference non-serializable services from my C# dialogs?&#34;中描述的替代方案之一。 Bot Framework Technical FAQ

的部分

最简单的方法是在您的字段中使用NonSerialized属性。

或者,您可以尝试通过将Reflection Serialization Surrogate添加到Autofac容器来注册。在您的global.asax中,尝试添加以下代码:

var builder = new ContainerBuilder();

builder.RegisterModule(new ReflectionSurrogateModule());

builder.Update(Conversation.Container);