我想让我的对话框与我的数据库一起工作。 如果我有我的对话是这样的:Microsoft Bot Framwork使用数据库结果
[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方法。任何人都可以看到明显的事情,为什么这不起作用?
QuestionGroup被标记为Serializable?如果你在对话框中持有引用,它们都需要是可序列化的(或者你需要明确地告诉对话框威胁它们不可序列化) –
QuestionGroup没有标记为可序列化(它来自另一个我无法控制的项目) 。我如何将它标记为不可序列化? – r3plica
尝试使用属性[NonSerialized]装饰字段。如果您需要跨不同消息使用该问题组,则可能会导致您将其引入功能中的其他问题,但会看到 –