熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

WCF中的Data Contract:WCF Data Contract Versioning

2013-11-13 10:33:07  來源: .NET編程 

  在系統開發過程中通過對Data Type添加額外的字段進而對其進行擴展是一個種很常見的場景本部分就作中介紹Data Contract的這種變化Service或者Client的Data Contract在本地添加一個新的Data Member會造成怎樣的影響WCF可以采用怎樣的機制來解決這種單方面Data Contract版本的改變

  我們同樣通過Dome來說話在這個Demo中我使用上面介紹的Order Processing的場景下面是整個Solution的結構(需要說明的是本片文章提供的Code片斷和Source Code都是基於VS 的)

  Service端ArtechDataContractVersioningService

  

  Data Contract

  using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemRuntimeSerialization;

  namespace ArtechDataContractVersioningService
{
[DataContract(Namespace=)]
public class Order
{
[DataMember(Order = )]
public Guid OrderID
{get;set;}

  [DataMember(Order = )]
public DateTime OrderDate
{ get; set; }

  [DataMember(Order = )]
public Guid SupplierID
{ get; set; }
}
}

  Service Contract 和Service Implementation: Process方法簡單地將Order對象返回到客戶端當Client接受到Service返回的Order對象後可以檢測和由它傳遞給Service的Order對象有什麼不同

  

  using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemServiceModel;

  namespace ArtechDataContractVersioningService
{
[ServiceContract]
public interface IOrderManager
{
[OperationContract]
Order Process(Order order);
}
}

  namespace ArtechDataContractVersioningService
{
public class OrderManagerService:IOrderManager
{
IOrderManager Members#region IOrderManager Members

  public Order Process(Order order)
{
return order;
}

  #endregion
}
}

  Client端

  

  Data Contract

  [DataContract(Name=OrderNamespace=)]
public class CustomOrder
{
[DataMember(Order = Name=OrderID)]
public Guid OrderNo
{ get; set; }

  [DataMember(Order = Name = SupplierID)]
public Guid SupplierNo
{ get; set; }

  [DataMember(Order = )]
public DateTime OrderDate
{ get; set; }       
}
}

  Program先創建一個Order對象向Console打印出Order的信息隨後以此作為參數調用Service最後將返回的Order對象的信息打印出來看看兩者之間的有何區別

  

  namespace ArtechDataContractVersioningClient
{
class Program
{
static void Main(string[] args)
{
ChannelFactory channelFactory = new ChannelFactory(orderManagerhttp);
IOrderManager orderManager = channelFactoryCreateChannel();

  try
{
CustomOrder order = new CustomOrder { OrderNo = GuidNewGuid()

  SupplierNo = GuidNewGuid() OrderDate = DateTimeToday

  ShippingAddress=Room E Airport Rd # Suzhou

  Jiangsu Province };
ConsoleWriteLine(The original order: \n{} orderToString());
order = orderManagerProcess(order);
ConsoleWriteLine(\n\nThe order processed by service: \n{}

  orderToString());
}
finally
{
(orderManager as IDisposable)Dispose();
}

  ConsoleRead();
}
}
}


通過上面的分析我們可以知道盡管就CLR Type的定義來講Service端的Order和Client端的CustomOrder具有很大的差異但是通過WCF Datacontract Attribute的適配他們是相互匹配的

  現在我們在Client端為Custom添加一個新的成員ShippingAddress通過重寫ToString方法

  

  namespace ArtechDataContractVersioningClient
{
[DataContract(Name=OrderNamespace=)]
public class CustomOrder
{
[DataMember(Order = Name=OrderID)]
public Guid OrderNo
{ get; set; }

  [DataMember(Order = Name = SupplierID)]
public Guid SupplierNo
{ get; set; }

  [DataMember(Order = )]
public DateTime OrderDate
{ get; set; }

  [DataMember(Order = )]
public string ShippingAddress
{ get; set; }

public override string ToString()
{
return stringFormat(Order No\t: {}\nSupplier No\t: {}\nOrder Date:\t:

  {}\nShipping Address: {} thisOrderNo thisSupplierNo

  thisOrderDate thisShippingAddress);
}
}
}

  我們來看看Client端程序運行的輸出結果

.NET編程免費提供,內容來源於互聯網,本文歸原作者所有。
推薦文章
Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.