2016-11-15 46 views
1

我想讓我的對話框與我的數據庫一起工作。 如果我有我的對話是這樣的: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方法。任何人都可以看到明顯的事情,爲什麼這不起作用?

+0

QuestionGroup被標記爲Serializable?如果你在對話框中持有引用,它們都需要是可序列化的(或者你需要明確地告訴對話框威脅它們不可序列化) –

+0

QuestionGroup沒有標記爲可序列化(它來自另一個我無法控制的項目) 。我如何將它標記爲不可序列化? – r3plica

+0

嘗試使用屬性[NonSerialized]裝飾字段。如果您需要跨不同消息使用該問題組,則可能會導致您將其引入功能中的其他問題,但會看到 –

回答

1

確保您的QestionGroup模型標記爲可序列化。

如果你不能讓它序列化,你還是希望自己的對話過程中引用它,你需要去與Bot Framework Technical FAQ的「How can I reference non-serializable services from my C# dialogs?」部分中描述的備選方案之一。

最簡單的是在您的字段中使用NonSerialized屬性。

或者,您可以嘗試通過將反射序列化代理添加到Autofac容器來註冊。在您的global.asax中,請嘗試添加以下代碼:

var builder = new ContainerBuilder(); 

builder.RegisterModule(new ReflectionSurrogateModule()); 

builder.Update(Conversation.Container); 
+0

我嘗試過之前使用Autofac,但最終無法工作,所以我把它拿出來了。我使用https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/AlarmBot來幫助我構建我的模塊。 – r3plica

+0

嘗試此示例:https://github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/demo-ContosoFlowers/ContosoFlowers。這很容易。然而;嘗試添加我發佈在Global.asax中的那些行,而不是使用NonSerialized –

+0

另一種替代方法是將QuestionGroup對象映射到一個可以控制的對象,並且可以確保它可序列化 –