C# 使用数据库结果的Microsoft Bot框架
我正在尝试使对话框与数据库一起工作。 如果我有这样的对话框:C# 使用数据库结果的Microsoft Bot框架,c#,botframework,C#,Botframework,我正在尝试使对话框与数据库一起工作。 如果我有这样的对话框: [Serializable] public class QuestionDialog : IDialog<object> { /// <summary> /// Start our response /// </summary> /// <param name="context">The current context</param>
[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);
}
}
[可序列化]
公共类问题对话框:IDialog
{
///
///开始我们的反应
///
///当前环境
///
公共异步任务StartAsync(IDialogContext上下文)
{
//转到下一个方法
Wait(StepOneAsync);
}
///
///收到消息后,我们执行此委托
///
///当前环境
///结果对象
///
专用异步任务StepOneAsync(IDialogContext上下文,IAwaitable结果)
{
//得到我们的活动
var活动=等待结果;
//问我们的第一个问题
等待上下文。PostAsync(“hi”);
//得到我们的答案
上下文。完成(本);
}
}
一切正常,我收到了预期的信息。然后我把它改成:
[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);
}
}
[可序列化]
公共类问题对话框:IDialog
{
//私有财产
私人IList组;
///
///开始我们的反应
///
///当前环境
///
公共异步任务StartAsync(IDialogContext上下文)
{
尝试
{
//创建我们的服务
var questionGroupService=newquestiongroupservice(newunitofwork());
//添加我们的问题组
这是。_questionGroups=wait questionGroupService.listallsync();
//转到下一个方法
Wait(StepOneAsync);
}捕获(例外情况除外)
{
}
}
///
///收到消息后,我们执行此委托
///
///当前环境
///结果对象
///
专用异步任务StepOneAsync(IDialogContext上下文,IAwaitable结果)
{
//得到我们的活动
var活动=等待结果;
//问我们的第一个问题
等待上下文。PostAsync(“hi”);
//得到我们的答案
上下文。完成(本);
}
}
而且它没有使用steponesync方法。有人能看到任何明显的东西来解释为什么这不起作用吗?确保您的QestionGroup模型被标记为可序列化 如果无法使其可序列化,并且仍希望在对话框中引用它,则需要使用中的“”部分中描述的备选方案之一 最简单的方法是在字段中使用属性 或者,您可以尝试通过将反射序列化代理添加到Autofac容器来注册反射序列化代理。在global.asax中,尝试添加以下代码:
var builder = new ContainerBuilder();
builder.RegisterModule(new ReflectionSurrogateModule());
builder.Update(Conversation.Container);
问题组是否标记为可序列化?如果您在对话框上保留引用,则所有引用都需要可序列化(或者您需要明确告诉对话框将其威胁为不可序列化)。QuestionGroup未标记为可序列化(它来自另一个我无法控制的项目)。如何将其标记为不可序列化?请尝试使用属性[NonSerialized]装饰该字段。我怀疑如果你需要在不同的消息中使用问题组,这可能会导致功能中出现另一个问题,但是will See这似乎是可行的,但当我等待用户响应时它会被置为空,这是我提到的副作用。您可以尝试使用on反序列化属性来重新水合对象,我以前确实尝试过使用Autofac,但结果不起作用,所以我将其取出。我正在使用帮助我构建模块。请尝试以下示例:。这更容易。然而;尝试添加我在Global.asax中发布的那些行,而不是使用NonSerializedAnother替代方法,这将把QuestionGroup对象映射到一个你可以控制的对象中,并且你可以确保它是序列化的Hehe太晚了,已经使用了我上面发布的链接并让Autofac工作了。是的,我可能会这样做