现在的位置: 首页 > 综合 > 正文

以XML为数据传输格式的Web service设计方案

2011年06月22日 ⁄ 综合 ⁄ 共 2972字 ⁄ 字号 评论关闭

 

 

本文的开发环境 Vss 2008

 

目录

      1 设计的背景

      2  技术点说明

      3  方案设计

      4  说明

 

1         设计的背景

一个公司有多套系统,多套系统是基于不同的平台,并在系统中包含了webFormWindows service 等形式的软件 ,同时每套系统都需要和公司的客户资料打交道。

在这样的情况下,我们设计了一个Web服务来统一的管理,对于Web 服务与客户端信息交互的方式通过固定格式的XML文档来实现  

 

2 技术点说明

.1 SoapWsdl区别

    Soap 全称为 simple object access protocal(简单对象访问协议)

 是以XML为基础通过Http,负责客户端和服务器端信息传递,

 

    Wsdl 全称是 web services description language web 服务描述语言)

 也是以XML为基础负责描述Web服务定义的接口的信息语言,描述web 服务的接口

 包含了接口定义的方法,参数,参数类型,等信息

 

.2 XML 结构文档XSD简要说明和XSD 验证方式

 XSD的全称为XML schema definitionXML 结构定义)它是一种描述XML结构的语言,用来描述XML文档的定义的元素,数据类型

 

vs2008 中利用xsd文档验证XML文件的结构

Xml 文件

<?xml version="1.0" encoding="utf-8"?>

<Books>

  <Book>

    <Name>软件设计原理</Name>

    <ISBN> 122-232-223-444-4444</ISBN>

    <Price>11.00</Price>

    <Author>王明</Author>

  </Book>

  <Book>

    <Name>软件设计模式</Name>

    <ISBN>122-111-223-2223</ISBN>

    <Price>13.00</Price>

    <Author>李斯</Author>

  </Book>

</Books>v

XSD文件

 

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name ="Books">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="Book" maxOccurs="unbounded">

          <xs:complexType>

            <xs:sequence>

              <xs:element name ="Name" type="xs:string"></xs:element>

              <xs:element name ="ISBN" type ="xs:string"></xs:element>

              <xs:element name ="Price" type="xs:decimal"></xs:element>

              <xs:element name="Author" type ="xs:string"></xs:element>

            </xs:sequence>

          </xs:complexType>

        </xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

</xs:schema>

对应的后台代码

添加命名空间

                 using System.Xml.Schema;

using System.Xml.Linq;

//Coding

            XmlSchemaCollection schemalist = new XmlSchemaCollection();

            schemalist.Add(string.Empty, "Books.xsd");

 

            XDocument doc = XDocument.Load("Books.xml");

            doc.Validate(schemalist,(sender,e)=>{

 

                //写日志

                throw e.Exception;

             });

.3 Web Server 的客户端权限验证

       在说明Web Server的客户端权限验证之前,我们先了解一下SoapHeader

      SoapHeader 是SOAP Envelope中的一部分 ,SoapHeader 一般的解决凌驾于具体的服务功能的问题,一般的是框架层面上的问题,比如说权限。下面是 web server 客户端权限的说明。

首先 Web server 是不带状态的,客户端按照权限票据的方式来获得权限,基本的流程是,服务端提供一个生成票据的端口的接口,在这个接口中包含了证明客户端身份的帐号和密码,验证通过,返回一个权限票据

      在客户端调用服务端具体的业务接口时通过SoapHeader传输权限票据,作为客户端的身份验证。

    SoapHeader 在服务器端简要代码用例

    /// <summary>

    /// Summary description for Service1

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

    // [System.Web.Script.Services.ScriptService]

    public class Service1 : System.Web.Services.WebService

    {

 

        public LoginUserInfo _userinfo ;

        [WebMethod]

        [SoapHeader("_userinfo")]

        public string ServerInterface()

        {

            if (Authentication())

            {

                return "No Pass The Authentication";

            }

            else

            {

                return "Pass the Authentication";

            }

           

            return "Hello World";

        }

抱歉!评论已关闭.