【发布时间】:2011-01-03 17:48:09
【问题描述】:
我的 WCF 服务有以下数据协定类:
[DataContract(Name = "MyClassDTO")]
public class MyClass
{
private string name = "Default Name";
[DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
}
当我使用 Visual Studio 的添加服务引用函数生成 WCF 服务引用时,生成的 DataContract 看起来像这样:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name = "MyClassDTO", Namespace = "xxx")]
[System.SerializableAttribute()]
public partial class MyClassDTO : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
{
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string NameField;
[System.Runtime.Serialization.DataMemberAttribute()]
public string Name
{
get
{
return this.NameField;
}
set
{
if ((object.ReferenceEquals(this.NameField, value) != true))
{
this.NameField = value;
this.RaisePropertyChanged("Name");
}
}
}
}
这意味着,默认值“默认名称”丢失并发生以下行为:
MyClassDTO mcdto = new MyClassDTO();
serviceClient.DoSomething(mcdto);
[OperationContract]
void DoSomething(MyClass mc){
mc.Name //<-- == null but I want it to be "Default Name"
}
有没有办法以这种方式配置数据合约,使定义的默认值“默认名称”不会丢失?
其他信息: 我使用服务引用没有在引用的程序集中重用类型,例如在客户端生成类 MyClassDTO 不知道服务器端类 MyClass
【问题讨论】:
标签: c# .net visual-studio-2008 wcf