WCF开发框架作为一款功能强大的跨平台解决方案,其中包含的操作方法和各种知识是相当繁杂的。我们需要不断的去熟练操作,才能掌握这一知识点。我们先来了解下WCF序列化依赖属性相关概念。#t#
众所周知.NetFramework中存在着两种依赖属性,他们也分别集成着不同但名称相同的依赖对象:
System.Windows.DependencyProperty:System.Windows.DependencyObject
System.Workflow.ComponentModel.DependencyProperty:System.Workflow.ComponentModel.DependencyObject
System.Window.DependencyProperty主要用于WPF中,我们可以以注册的形式声明这种‘特别’的属性,声明中可以设置Metadata,PropertyChangeCallBack...等等,让我能用几句简单的代码来实现强大的WPF操作。
System.Workflow.ComponentModel.DependencyProperty相对于前者,是一个简化版本,只可以在声明中可以设置Metadata,但对于WorkflowFoundation这就足够用了。
两种依赖属性对各自的技术,都不同程度的提供了很好的支持,让我们在实际开发中能够更高效的书写代码,但是我们能不能像一般的属性那样随意声明,并运用?至少在WCF序列化依赖属性中我们很难使用这种特殊的属性。
以工作流中的System.workflow.ComponentModel.DependencyObject为例
如果我们想像一般自定义类那样,在声明完DataContract和DataMember后便可在基于WCF的应用程序中应用,会遇到UserData这个继承于IDictionary的讨厌属性在WCF中无法序列化。如:
[DataContract]
public class WorkFlowParameter :
DependencyObject
{
public static readonly Dependency
Property IDProperty =
DependencyProperty.Register("ID",
typeof(Guid), typeof(WorkFlowParameter),
new PropertyMetadata("UserDefinedProperty"));
[DataMember]
public Guid ID
{
get { return (Guid)GetValue(IDProperty); }
set { SetValue(IDProperty, value); }
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
像这样一个看起来很平常的类,在WCF序列化依赖属性应用中,我们只能无语了。
为了使得包含依赖属性的自定义类能在WCF中正常使用
我们可以以下面的步骤自己动手写序列化方法
1.在自定义类中继承ISerializable接口,并实现构造函数以及GetObjectData方法
如:
public class WorkFlowParameter :
DependencyObject,ISerializable
{
//在Deserialize时使用
public WorkFlowParameter(SerializationInfo
info, StreamingContext context)
{
ID = new Guid (info.GetString("ID"));
ParameterName = info.GetString("ParameterName");
}
//在Serialize时调用,把除了UserData
以外我们自定义的属性添加进来进行序列化
public void GetObjectData(SerializationInfo
info, StreamingContext context)
{
IList<DependencyProperty> properties =
DependencyProperty.FromType(this.GetType());
if(properties.Count > 0)
{
foreach(DependencyProperty property
in properties)
{
if(property.Name != "UserData")
{
info.AddValue(property.Name,
GetValue(property));
}
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
2.经过我们自定义序列化后,我们可以正常使用了
如果你遇到类型XXXX不能为 ISerializable,且不能具有 DataContractAttribute 属性这时候我们需要在WCF序列化依赖属性中,我们可以把类中的
[DataContract]去掉
[DataContract]//去掉
public class Work
FlowParameter :
DependencyObject
- 1.
- 2.
- 3.
- 4.
- 5.
再试试,大功告成了。呵呵。