2017-08-16 15 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()); 
});