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

来源:互联网 发布:ajax和json的区别 编辑:程序博客网 时间:2024/05/14 07:21

本文的开发环境 Vss 2008

<script type="text/javascript"><!--google_ad_client = "pub-2487365762057517";google_ad_slot = "5709514408";google_ad_width = 200;google_ad_height = 200;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

目录

      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";

        }

 

        /// <summary>

        /// Authentication whether  the LoginUser have purview

        /// </summary>

        /// <returns></returns>

        private bool Authentication()

        {

            if (_userinfo.Name== "Admin") //this is Example

                return true ;

            else

                return false ;

        }

    }

 

 

    /// <summary>

    /// 登陆用户信息

    /// </summary>

    public class LoginUserInfo : SoapHeader

    {

        /// <summary>

        /// 用户名称

        /// </summary>

        public string Name { get; set; }

 

        /// <summary>

        /// 用户密码

        /// </summary>

        public string PassWord { get; set; }

 

    }

    SoapHeader 在客户端的简要代码用例

   

           ServiceSoapClient Client = new ServiceSoapClient();

           Client._userinfo.Name = "admin";

           Client._userinfo.PassWord = "passWord";

           Client.Authentication();

   

    

.4 Linq 查询XML文档并返回查询结果的对象

                    IList<Book> list = (from p in doc.Root.Elements("Book")

                                select new Book

                                {

                                    Name = p.Element("Name").Value,

                                    ISBN = p.Element("ISBN").Value,

                                    Price =Convert.ToDecimal(p.Element("Price").Value),

                                    Author = p.Element("Author").Value

                                }).ToList();

  

3 方案设计

3.1 结构图

未命名.JPG
                       
Web
服务结构图

 

下面以添加客户信息为例 各层之间的定义

3.1.1 服务接口定义

包含权限票据接口和正常的服务功能接口

Code


3.1.2 XML解析器,转化对象

将字符串转化成对应的对象

Code

 

3.1.3 业务层

具体的业务

添加客户信息

Code