2017-08-16 23 views
1

我收到以下錯誤:,捕捉環境匿名方法閉包不能序列

Exception: anonymous method closures that capture the environment are not serializable, consider removing environment capture or using a reflection serialization surrogate: assistant.dialogs.Forms.SupportRequest+<>c__DisplayClass7_0'

,當我嘗試設置內財產鏈。

你能幫忙嗎?

我的代碼:

public enum SystemSelection { SharePoint, BizTalk, Azure, Office365 }; 
public enum RequestType { Bug, SupportRequest, Question }; 
public enum Importance { Blocking, High, Medium, Low }; 

[Serializable] 
class Declaration 
{ 
    public string Type; 
    public string Amount; 
    public string Date; 

    public static IForm<Declaration> BuildForm() 
    { 
     return new FormBuilder<Declaration>() 
       .Message("Add a declaration") 
       .Build(); 
    } 
} 

[Serializable] 
class SupportRequest 
{ 

    public SystemSelection? SystemSelection; 
    public RequestType? RequestType; 
    public Importance? Importance; 

    public Declaration Declaration; 

    public static IForm<SupportRequest> BuildForm() 
    { 
     return new FormBuilder<SupportRequest>() 
       .Message("Welcome to the simple support bot!") 
       .AddRemainingFields() 
       .Build(); 
    } 

    internal static IDialog<SupportRequest> MakeRootDialog3() 
    { 
     SupportRequest t = null; 
     var dlg = Chain.ContinueWith(FormDialog.FromForm(SupportRequest.BuildForm), 
          async (context, res) => 
          { 
           t = await res; 
           return Chain.ContinueWith<Declaration, SupportRequest>(FormDialog.FromForm(Declaration.BuildForm), 
                  async (context2, res2) => 
                  { 
                   t.Declaration = await res2; 
                   return Chain.Return(t); 
                  }); 
          }); 

     return dlg; 
    } 
} 

回答

1

因爲匿名方法不能序列和每個docs你需要確保所有的對話都是可序列化這是預期:

Ensure that all dialogs are serializable. This can be as simple as using the [Serializable] attribute on your IDialog implementations. However, be aware that anonymous method closures are not serializable if they reference their outside environment to capture variables. The Bot Framework supports a reflection-based serialization surrogate to help serialize types that are not marked as serializable.

你將不得不更換​​繼續你正在定義一個匿名方法與非匿名方法。

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

Conversation.UpdateContainer(builder => 
{ 
    builder.RegisterModule(new ReflectionSurrogateModule()); 
});