通过第一部分的介绍,我们可以体会到,WCF 的Data Contract在CLR Type和Neutral Contract之间搭建了一座桥梁,弥合了.NET世界和厂商中立世界的差异。通过WCF Data Contract我们将CLR Data Type暴露成一个厂商中立的数据结构的描述,同样通过WCF Data Contract我们将一个现有的CLR Data Type和既定的Neutral contract进行适配。
在.NET中,基于Primary Type,比如Int32,String等等,他们具有一个简单的默认的序列化方式和结构,可以说他们不需要Data Contract。接下来我们主要讨论的是一些相对比较特殊的、完全基于.NET的Data Type,比如Generic、Collection,和Dictionary。首先,我们结合例子来谈谈基于Generic的Data Type的Data Contract。
假设我们需要创建一个用于处理一些单据(Bill)的Service,比如如Order Bill、Sales Bill等。一般的单据都有一个单据头(Header)和明细(Detail)列表,为此我们创建了一个Generic的Bill。并
namespace
Artech.SpecialDataContract.Contract
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DataContract]
public class Bill<THeader, TDetail>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public THeader Header
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public IList<TDetail> DetailList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataContract]
public class OrderHeader
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public Guid OrderID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public DateTime OrderDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataContract]
public class OrderDetail
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public Guid ProductID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public int Quantity
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
为处理订单单据创建了机遇订单的Header和Detail。
对于一个Neutral Service Contract和Neutral Data Contract本身是不可能支持Generic的,也就是Neutral Contract只能是对一个具体的CLR Type的体现。所以在定义Service Contract的时候,对于那些包含Generic Type作为参数或者返回值得Operation,我们必须指定一个具体的Data Type。所以我们创建了如下一个IBillManager Service Contract:
namespace
Artech.SpecialDataContract.Contract
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[ServiceContract]
public interface IBillManager
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[OperationContract]
void Procss(Bill<OrderHeader, OrderDetail> orderBill);
}
}
如何我们现在
Host
基于这样一个
Contract
的
Service
,你猜我们作为参数的数据类型将会如何体现的。
通过WSDL,我们会发现该Service的Data Contract将会以下面一段XSD的方式来呈现:
<?
xml version="1.0" encoding="utf-8"
?>
<
xs:schema
elementFormDefault
="qualified"
targetNamespace
="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
xmlns:xs
="http://www.w3.org/2001/XMLSchema"
xmlns:tns
="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
xmlns:ser
="http://schemas.microsoft.com/2003/10/Serialization/"
>
<
xs:import
schemaLocation
="http://artech/Artech.SpecialDataContract/BillManagerService.svc?xsd=xsd1"
namespace
="http://schemas.microsoft.com/2003/10/Serialization/"
/>
<
xs:complexType
name
="BillOfOrderHeaderOrderDetailLZ9Dq20o"
>
<
xs:annotation
>
<
xs:appinfo
>
<
GenericType
Name
="BillOf{0}{1}{#}"
Namespace
="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
xmlns
="http://schemas.microsoft.com/2003/10/Serialization/"
>
<
GenericParameter
Name
="OrderHeader"
Namespace
="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
/>
<
GenericParameter
Name
="OrderDetail"
Namespace
="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
/>
</
GenericType
>
</
xs:appinfo
>
</
xs:annotation
>
<
xs:sequence
>
<
xs:element
minOccurs
="0"
name
="DetailList"
nillable
="true"
type
="tns:ArrayOfOrderDetail"
/>
<
xs:element
minOccurs
="0"
name
="Header"
nillable
="true"
type
="tns:OrderHeader"
/>
</
xs:sequence
>
</
xs:complexType
>
<
xs:element
name
="BillOfOrderHeaderOrderDetailLZ9Dq20o"
nillable
="true"
type
="tns:BillOfOrderHeaderOrderDetailLZ9Dq20o"
/>
<
xs:complexType
name
="ArrayOfOrderDetail"
>
<
xs:sequence
>
<
xs:element
minOccurs
="0"
maxOccurs
="unbounded"
name
="OrderDetail"
nillable
="true"
type
="tns:OrderDetail"
/>
</
xs:sequence
>
</
xs:complexType
>
<
xs:element
name
="ArrayOfOrderDetail"
nillable
="true"
type
="tns:ArrayOfOrderDetail"
/>
<
xs:complexType
name
="OrderDetail"
>
<
xs:sequence
>
<
xs:element
minOccurs
="0"
name
="ProductID"
type
="ser:guid"
/>
<
xs:element
minOccurs
="0"
name
="Quantity"
type
="xs:int"
/>
</
xs:sequence
>
</
xs:complexType
>
<
xs:element
name
="OrderDetail"
nillable
="true"
type
="tns:OrderDetail"
/>
<
xs:complexType
name
="OrderHeader"
>
<
xs:sequence
>
<
xs:element
minOccurs
="0"
name
="OrderDate"
type
="xs:dateTime"
/>
<
xs:element
minOccurs
="0"
name
="OrderID"
type
="ser:guid"
/>
</
xs:sequence
>
</
xs:complexType
>
<
xs:element
name
="OrderHeader"
nillable
="true"
type
="tns:OrderHeader"
/>
</
xs:schema
>
对于不习惯看XSD的朋友,我们可以通过Add Service Reference的方式创建本地的Proxy file,借助生成的与之对应的Class来看看这个XSD最终呈现的结构:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"
System.Runtime.Serialization
"
,
"
3.0.0.0
"
)]
[System.Runtime.Serialization.DataContractAttribute(Name
=
"
BillOfOrderHeaderOrderDetailLZ9Dq20o
"
, Namespace
=
"
http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract
"
)]
[System.SerializableAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
partial
class
BillOfOrderHeaderOrderDetailLZ9Dq20o :
object
, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
{
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private Artech.SpecialDataContract.Client.BillManagerService.OrderDetail[] DetailListField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private Artech.SpecialDataContract.Client.BillManagerService.OrderHeader HeaderField;
[global::System.ComponentModel.BrowsableAttribute(false)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.extensionDataField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Artech.SpecialDataContract.Client.BillManagerService.OrderDetail[] DetailList
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.DetailListField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((object.ReferenceEquals(this.DetailListField, value) != true))
{
this.DetailListField = value;
this.RaisePropertyChanged("DetailList");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Artech.SpecialDataContract.Client.BillManagerService.OrderHeader Header
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.HeaderField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((object.ReferenceEquals(this.HeaderField, value) != true))
{
this.HeaderField = value;
this.RaisePropertyChanged("Header");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"
System.Runtime.Serialization
"
,
"
3.0.0.0
"
)]
[System.Runtime.Serialization.DataContractAttribute(Name
=
"
OrderHeader
"
, Namespace
=
"
http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract
"
)]
[System.SerializableAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
partial
class
OrderHeader :
object
, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
{
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private System.DateTime OrderDateField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private System.Guid OrderIDField;
[global::System.ComponentModel.BrowsableAttribute(false)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.extensionDataField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.DateTime OrderDate
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.OrderDateField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((this.OrderDateField.Equals(value) != true))
{
this.OrderDateField = value;
this.RaisePropertyChanged("OrderDate");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.Guid OrderID
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.OrderIDField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((this.OrderIDField.Equals(value) != true))
{
this.OrderIDField = value;
this.RaisePropertyChanged("OrderID");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"
System.Runtime.Serialization
"
,
"
3.0.0.0
"
)]
[System.Runtime.Serialization.DataContractAttribute(Name
=
"
OrderDetail
"
, Namespace
=
"
http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract
"
)]
[System.SerializableAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
partial
class
OrderDetail :
object
, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
{
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private System.Guid ProductIDField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private int QuantityField;
[global::System.ComponentModel.BrowsableAttribute(false)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.extensionDataField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public System.Guid ProductID
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.ProductIDField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((this.ProductIDField.Equals(value) != true))
{
this.ProductIDField = value;
this.RaisePropertyChanged("ProductID");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Quantity
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.QuantityField;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((this.QuantityField.Equals(value) != true))
{
this.QuantityField = value;
this.RaisePropertyChanged("Quantity");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
为了使大家一眼就能了解整个结构,我对上面的代码进行简化:
namespace
Artech.SpecialDataContract.Client.BillManagerService
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DataContract]
public class BillOfOrderHeaderOrderDetailLZ9Dq20o
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public OrderHeader Header
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public OrderDetail[] DetailList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataContract]
public class OrderHeader
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public Guid OrderID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public DateTime OrderDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataContract]
public class OrderDetail
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[DataMember]
public Guid ProductID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public int Quantity
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
我们可以通过上面的code,注意到下面的细节:
· Generic class Bill<THeader, TDetail>没有了,取而代之的是使用了具体OrderHeader和OrderDetial的新的非Generic class:BillOfOrderHeaderOrderDetailLZ9Dq20o。正如我们在上面所说,Neutral Contract根本就不知道Generic为何物。
· 新的Class name的名称很难看,它有下面几个部分组成:Bill(Generic Type Name)+ Of + OrderHeader(Generic Type的第一个类型参数对应的具体类型名称)+OrderDetail(Generic Type的第二个类型参数对应的具体类型名称)+lLZ9Dq20o(Generic Type参数类型Namespace的Hash Value)。
· 原本使用IList表示的DetailList变成了Array(public OrderDetail[] DetailList),这个将在和面的部分介绍。
我想你也不能容忍生成的如此冗长、甚至没有太大意义的Class name。我们有办法生成一个友好的名称。那就是显示指定Data Contract的Name:
[DataContract(Name
=
"
OrderBill
"
)]
public
class
Bill
<
THeader, TDetail
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DataMember]
public THeader Header
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public IList<TDetail> DetailList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
现在对应的Data Contract Name将变成我们指定的名称。
public partial class OrderBill : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {}
但是上面的做法是不对的。原因很简单,如果我们在Service中添加一个处理Sales Bill的Operation(当然我们会添加两个额外的Header和Detail:SalesHeader和SalesDetail):
[ServiceContract]
public
interface
IBillManager
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[OperationContract(Name=”ProcessOrderBill”)]
void Procss(Bill<OrderHeader, OrderDetail> orderBill);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[OperationContract(Name =”ProcessSalesBill”)]
void Procss(Bill<SalesHeader, SalesDetail> salesBill);
}
很显然,WCF需要为Order Bill和Sales Bill创建两个Data Contract,但是现在你却把他们的名称显示地限定到一个固定的名称,很显然这会造成命名的冲突。如果你通过Browser试图访问Service,你会得到如下的Error:
The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
所以WCF需要为此提供这样的机制:基于不同的泛型类型参数生成不同Data Contract Name,这样才能解决命名冲突。我们可以稍微修改一下Data Contract 的定义就可以了:
[DataContract(Name
=
"
Bill_{0}_{1}
"
)]
public
class
Bill
<
THeader, TDetail
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DataMember]
public THeader Header
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public IList<TDetail> DetailList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
其中{0}和{1}分别代表第一个泛型类型参数和第二个泛型类型参数的名称,一次类推,你可以根据参数类型的个数设置{2}{3}…
这样我们生成的两个DataContract的名称为:Bill_OrderHeader_OrderDetail和Bill_SalesHeader_SalesDetail。
但是这并没有根本解决问题,如果在我现在不同的Namespace中创建了两个OrderHeader和OrderDetail呢?这无疑在.NET中是合法的,但是对于DataContract有有可能出现命名冲突。
这也就是为什么WCF默认机制下会为Data Contract Name添加一个额外hash value的原因。其实你也可以以你自己的方式添加这个Hash value:
[DataContract(Name
=
"
Bill_{0}_{1}_{#}
"
)]
public
class
Bill
<
THeader, TDetail
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DataMember]
public THeader Header
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember]
public IList<TDetail> DetailList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ get; set; }
}
上面的
{#}
就代表这样一个
Hash Value,
我想你会想到现在生成的
Data Contract Name
象什么样子:
Bill_OrderHeader_OrderDetail_LZ9Dq20o
和
Bill_SalesHeader_SalesDetail_LZ9Dq20o