WCF 第二章 契约 定义类的层次结构

本文探讨了复杂类型在代码中的实现方式,通过类和继承关系定义,并展示了如何使用WCF进行序列化和反序列化操作。文章还详细介绍了XML元数据的生成及其在SOAP消息体中的应用。
复杂类型一般在代码中以类的形式实现。复杂类更进一步通过增加特殊结构的继承关系来定义。这种方式,一个通用类型比如”price” 可以派生出为一个更加特殊的类型如”stock price” 或者 “house price”.WCF支持通过在WSDL中合适的表示的类的继承关系,在类结构和XML之间序列化和反序列化它们同时从每个类中取出属性并加入到一个集合中。
  在列表2.17中,类Price由三个元素和一个子类组成。StockPrice,继承自Price.命名空间应用到两个类上所以它们在XML中由完全合法的名字。每个元素保留自己的命名空间。
 
列表2.17 使用数据契约定义类的层次结构

   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Stock
{
[DataContract(Namespace
= " http://EssentialWCF/Price/ " )]
public class Price
{
[DataMember]
public double CurrentPrice;
[DataMember]
public DateTime CurrentTime;
[DataMember]
public string Currency;
}

[DataContract(Namespace
= " http://EssentialWCF/StockPrice " )]
public class StockPrice : Price
{
[DataMember]
public string Ticker;
[DataMember]
public long DailyVolume;
[DataMember]
public double DailyChange;
}
}
生成这两段XML元数据是用来支持列表2.18中显示的基础关系。首先显示Price XML元数据,其次显示StockPrice XML元数据。注意StockPrice导入Price元数据。注意在XSD中,所有的元素都是用minOccurs=0来定义,因为在代码中它们都没有使用[isRequired=true]来定义属性。
列表2.18 XML元数据中定义的类的层次结构

   
<? xml version="1.0" encoding="utf-8" ?>
< xs:schema xmlns:tns =http://EssentialWCF/Price/
elementFormDefault ="qualified"
targetNamespace =http://EssentialWCF/Price/
xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:complexType name ="Price" >
< xs:sequence >
< xs:element minOccurs ="0" name ="Currency"
nillable ="true" type ="xs:string" />
< xs:element minOccurs ="0"
name ="CurrentPrice" type ="xs:double" />
< xs:element minOccurs ="0"
name ="CurrentTime" type ="xs:dateTime" />
</ xs:sequence >
</ xs:complexType >
< xs:element name ="Price"
nillable ="true" type ="tns:Price" />
</ xs:schema >

   
<? xml version="1.0" encoding="utf-8" ?>
< xs:schema xmlns:tns ="http://EssentialWCF/StockPrice" el
ementFormDefault ="qualified"
xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:import namespace ="http://EssentialWCF/Price/" />
< xs:complexType name ="StockPrice" >
< xs:complexContent mixed ="false" >
< xs:extension xmlns:q1 ="http://EssentialWCF/Price/" base ="q1:Price" >
< xs:sequence >
< xs:element minOccurs ="0"
name ="DailyChange" type ="xs:double" />
< xs:element minOccurs ="0"
name ="DailyVolume" type ="xs:long" />
< xs:element minOccurs ="0"
name ="Ticker" nillable ="true" type ="xs:string" />
</ xs:sequence >
</ xs:extension >
</ xs:complexContent >
</ xs:complexType >
< xs:element name ="StockPrice" nillable ="true" type ="tns:StockPrice" />
</ xs:schema >
  一个序列化的StockPrice类型的SOAP消息体在列表2.19中显示。注意Price和StockPrice的命名空间完全通过SOAP消息体被从列表2.17的代码搬到列表2.18中的XML元数据中。
列表2.19 SOAP消息体中序列化的类层次结构

   
< s:Body >
< GetPriceResponse xmlns ="http://EssentialWCF/FinanceService/" >
< GetPriceResult xmlns:a ="http://EssentialWCF/StockPrice"
xmlns:i
="http://www.w3.org/2001/XMLSchema-instance" >
< Currency xmlns ="http://EssentialWCF/Price" > Dollars </ Currency >
< CurrentPrice xmlns ="http://EssentialWCF/Price" > 100 </ CurrentPrice >
< CurrentTime xmlns ="http://EssentialWCF/Price" > 2006-12-13T21:18:51.313-05:00 </ CurrentTime >
< a:DailyChange > 0.012345 </ a:DailyChange >
< a:DailyVolume > 450000 </ a:DailyVolume >
< a:Ticker > msft </ a:Ticker >
</ GetPriceResult >
</ GetPriceResponse >
</ s:Body >

转载于:https://www.cnblogs.com/danielWise/archive/2011/06/23/2088460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值