WebService开发心得

      又好久没有上来写文章了(为什么要说又呢???),最近比较忙,不过都是乱七八糟的事情,哎!
前一阵公司有个项目,用VS 2005开发的Web Service,有点心得,写下来,与君共勉!!!

      这次完全采用Schema来定义数据结构,以一个书目查询的服务为例!那么我们先来定义一个关于书籍信息的数据结构的Schema:

<? xml version="1.0" encoding="UTF-8" ?>
< xs:schema  xmlns:xs ="http://www.w3.org/2001/XMLSchema"  xmlns:in ="http://www.bookinfo.com/bookinfo/"  targetNamespace ="http://www.bookinfo.com/bookinfo/"  elementFormDefault ="qualified"  attributeFormDefault ="unqualified" >
    
< xs:complexType  name ="BookInfoType" >
        
< xs:sequence >
            
< xs:element  name ="BookName"  type ="xs:string" />
            
< xs:element  name ="AuthorName"  type ="xs:string" />
            
< xs:element  name ="Price"  type ="xs:float" />
            
< xs:element  name ="Publish"  type ="xs:string" />
            
< xs:element  name ="PublishDate"  type ="xs:dateTime" />
        
</ xs:sequence >
    
</ xs:complexType >
    
< xs:element  name ="BookInfo"  type ="in:BookInfoType" />
</ xs:schema >


      之后我们再来定义一下Request协议的Schema

<? xml version="1.0" encoding="utf-8" ?>
< xs:schema  xmlns:xs ="http://www.w3.org/2001/XMLSchema"  xmlns:ns1 ="http://www.bookinfo.com/bookinforequest/"  targetNamespace ="http://www.bookinfo.com/bookinforequest/"  elementFormDefault ="qualified"  attributeFormDefault ="unqualified" >
    
< xs:element  name ="QueryInfo" >
        
< xs:complexType >
            
< xs:sequence >
                
< xs:element  name ="BookName"  type ="xs:string"  minOccurs ="0" />
                
< xs:element  name ="AuthorName"  type ="xs:string"  minOccurs ="0" />
                
< xs:element  name ="PublishDate"  minOccurs ="0" >
                    
< xs:complexType >
                        
< xs:sequence >
                            
< xs:element  name ="StartDate"  type ="xs:dateTime" />
                            
< xs:element  name ="EndDate"  type ="xs:dateTime"  minOccurs ="0" />
                        
</ xs:sequence >
                    
</ xs:complexType >
                
</ xs:element >
            
</ xs:sequence >
        
</ xs:complexType >
    
</ xs:element >
</ xs:schema >

      好了,Request协议的结构定义完了,接下来就是Resposne的Schema了!

 

<? xml version="1.0" encoding="utf-8" ?>
< xs:schema  xmlns:xs ="http://www.w3.org/2001/XMLSchema"  xmlns:in ="http://www.bookinfo.com/bookinfo/"  xmlns:r ="http://www.bookinfo.com/bookinforesponse/"  targetNamespace ="http://www.bookinfo.com/bookinforesponse/"  elementFormDefault ="qualified"  attributeFormDefault ="unqualified" >
    
<!--  引用Schema  -->
    
< xs:import  namespace ="http://www.bookinfo.com/bookinfo/"  schemaLocation ="D:\文档\Document\BookInfo.xsd" />
    
<!-- 引用Schema结束 -->
    
<!-- 数据类型定义 -->
    
<!-- 书籍信息列表 -->
    
< xs:complexType  name ="BookInfoListType" >
        
< xs:sequence >
            
< xs:element  name ="BookInfo"  type ="in:BookInfoType"  maxOccurs ="unbounded" />
        
</ xs:sequence >
    
</ xs:complexType >
    
<!-- 书籍信息列表结束 -->
    
<!-- 数据类型定义结束 -->
    
<!-- 内容 -->
    
< xs:element  name ="BookInfoList"  type ="r:BookInfoListType" />
    
<!-- 内容 -->
</ xs:schema >


      Schema都定义完成了,我们可以使用VS自带的工具xsd.exe来生成XML序列化过的类文件了,xsd.exe会将Schema里的文档部分的根节点生成类,并将其它的element及attribute序列化为子类及属性


      将生成的类文件加到我们的项目里,开始WebService的编写

      首先是WebMethod的定义

      [WebMethod]
      public BookInfoListType BookInfoRequest(QueryInfo query) 
      {
           return null;      
      }
   
      这里比较特殊的就是方法的返回数据类型及参数数据类型,返回的数据类型为BookInfoListType,而参数的数据类型为QueryInfo,这两个类型,都是我们用Schema生成的类型,可以根据类实例属性的值,生成符合Schema要求的XML串,也可以自动解析符合Schema定义的XML字符串,将XML串里的数据信息,赋给类实例的相应属性
      当我们调用WebService时,.Net会将Post过来的XML字符串进行解析,并将XML字符串里的值,赋给类实例里的相应的属性,我们再通过取得的属性值进行相关操作
      在设置返回数据类型的类实例时,可以将想要返回的值赋给类实例,之后return这个实例就可以了,.net会
将这个XML序列化过的类实例生成XML串,并Response

      附上程序实例 WebSite



 

你可能感兴趣的:(webservice)