简体   繁体   English

由于复杂的对象,Workflow SerializationException

[英]Workflow SerializationException because of complex object

I got the following exception: 我得到以下异常:

System.Workflow.Runtime.Hosting.PersistenceException: Type 'Microsoft.SharePoint.SPWeb' in Assembly 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable. System.Workflow.Runtime.Hosting.PersistenceException:在程序集“Microsoft.SharePoint,Version = 12.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c”中键入“Microsoft.SharePoint.SPWeb”未标记为可序列化。 —> System.Runtime.Serialization.SerializationException: Type 'Microsoft.SharePoint.SPWeb' in Assembly 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable - > System.Runtime.Serialization.SerializationException:在程序集“Microsoft.SharePoint,Version = 12.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c”中键入“Microsoft.SharePoint.SPWeb”未标记为可序列化

The error came from here: 错误来自这里:

public sealed partial class MyWorkflow : StateMachineWorkflowActivity
{
    public SPWorkflowActiviationProperties workflowProperties = new SPWorkflowActivationProperties();
    private SPWeb spWebtemp;

    private SPWeb spWeb
    {
        get { return spWebtemp ?? (spWebtemp = workflowProperties.Web); }
    }

    ...

There are two blog posts I found: 我发现有两篇博文:

There is one solution to be found to this problem: Not have complex member objects as global variables , but as a local variables - ie declare SPWeb locally (workflowProperties.Web) instead of on a global level. 有一个解决方案可以解决这个问题: 没有复杂的成员对象作为全局变量 ,而是作为局部变量 - 即在本地声明SPWeb(workflowProperties.Web)而不是在全局级别。

So I would have to redeclare spWeb in every method I am using - which I deem rather ugly. 因此,我必须在我正在使用的每种方法中重新声明spWeb - 我认为这相当丑陋。

What I also tried is this: 我也尝试过这个:

...
[NonSerialized]
private SPWeb spWebtemp;

private SPWeb spWeb
{
    get { return spWebtemp ?? (spWebtemp = workflowProperties.Web); }
}
...

==> no more serialization exception! ==>没有更多的序列化异常!

Are there any negative implications when using the NonSerialized attribute on this field? 在此字段上使用NonSerialized属性时是否有任何负面影响?
Or in other words - what are the implications? 换句话说 - 有什么含义?

Why don't you simply do : 你为什么不这样做:

private SPWeb spWeb
{
    get { return workflowProperties.Web; }
}

the lazy load of the spweb object is already handled by the properties property spweb对象的延迟加载已由properties属性处理

This looks like it also would also work (the OnDeserialized Attribute): 看起来它也可以工作(OnDeserialized属性):

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

[EDIT] [编辑]

I haven't tested this, but I'm thinking something like this: 我没有测试过这个,但我想的是这样的:

public sealed partial class MyWorkflow : StateMachineWorkflowActivity
{

public SPWorkflowActiviationProperties workflowProperties = new SPWorkflowActivationProperties();

[NonSerialized()]
private SPWeb spWebtemp;

private SPWeb spWeb
{
    get { return spWebtemp ?? (spWebtemp = workflowProperties.Web); }
}

[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
    spWebTemp = workflowProperties.Web;
}


...

Also, why not initialize the spWebtemp object in the constructor, or is that not possible with workflows? 另外,为什么不在构造函数中初始化spWebtemp对象,或者工作流无法实现?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM