以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 设计的背景
一个公司有多套系统,多套系统是基于不同的平台,并在系统中包含了web,Form,Windows service 等形式的软件 ,同时每套系统都需要和公司的客户资料打交道。
在这样的情况下,我们设计了一个Web服务来统一的管理,对于Web 服务与客户端信息交互的方式通过固定格式的XML文档来实现
2 技术点说明
2.1 Soap和Wsdl区别
Soap 全称为 simple object access protocal(简单对象访问协议)
是以XML为基础通过Http,负责客户端和服务器端信息传递,
Wsdl 全称是 web services description language (web 服务描述语言)
也是以XML为基础负责描述Web服务定义的接口的信息语言,描述web 服务的接口
包含了接口定义的方法,参数,参数类型,等信息
2.2 XML 结构文档XSD简要说明和XSD 验证方式
XSD的全称为XML schema definition(XML 结构定义)它是一种描述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;
});
2.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();
2.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 结构图
Web 服务结构图
下面以添加客户信息为例 各层之间的定义
包含权限票据接口和正常的服务功能接口
/// <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 AuthenticationTicket ClientTicket;
[WebMethod]
[SoapHeader("ClientTicket")]
public string AddCustomInf(string XMLString)
{
if (!Authentication)
return string.Empty;
//解析并生成CustomInf对象
IList<CustomInf> list = XMLParser.Instance.Parser(XMLString);
CustomInfOper oper = new CustomInfOper();
//保存客户信息
foreach (CustomInf inf in list)
{
oper.AddCustomInf(inf);
}
}
/// <summary>
/// Authentication whether the LoginUser have purview
/// </summary>
/// <returns></returns>
private bool Authentication()
{
bool returnvalue=false;
if (ClientTicket != null)
{
//数据库验证票据是否有效
returnvalue = true;
}
return returnvalue;
}
/// <summary>
/// 根据帐号和密码,生成验证票据,并返回
/// </summary>
/// <param name="UserName">用户名称</param>
/// <param name="PassWord">密码</param>
/// <returns></returns>
public string GetAuthenticationTicket(string UserName,string PassWord)
{
string AuthenticationTicket = string.Empty;
//验证权限,并返回票据
return AuthenticationTicket;
}
}
/// <summary>
/// 验证票据
/// </summary>
public class AuthenticationTicket : SoapHeader
{
public string Ticket { get; set; }
}
将字符串转化成对应的对象
1internal class XMLParser
2
3 {
5 private XMLParser()
7 { }
8
9 private static XMLParser _instance;
11 internal static XMLParser Instance
13 {
15 get
17 {
19 if (_instance == null)
21 _instance = new XMLParser();
23 return _instance;
25 }
27 }
32
33 internal IList<CustomInf> Parser(string XMLString)
34
35 {
37 try
39 {
43 //验证XML文档结构是否和XSD文件一致
45 XmlSchemaSet schemas = new XmlSchemaSet();
47 schemas.Add("", System.Web.HttpContext.Current.Request.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + "/App_LocalResources/Template.xsd"));
48
49 XDocument doc = XDocument.Load(XmlReader.Create(new StringReader(XMLString)));
50
51 bool errors = false;
52
53 doc.Validate(schemas, (o, e) =>
54
55 {
57 errors = true;
58
59 throw e.Exception;
60
61 });
68
69 //获取对应数据并转化成对应对象
71 IList<CustomInf> list = (from p in doc.Root.Elements("CustomInf")
73 select new CustomInf
75 {
77 CustomName = p.Element("CustomName").Value,
79 Telephone = p.Element("Telephone").Value,
81 Address = p.Element("Address").Value,
85 }).ToList();
89 return list;
93 }
94
95 catch (XmlException ex) //异常处理写日志
97 {
99 //写日志
101 }
103 catch (XmlSchemaValidationException ex)
105 {
107 //写日志
109 }
110
111 catch (Exception ex)
113 {
115 //写日志
117 }
121 return null;
123 }
125 }
126
具体的业务
添加客户信息
public class CustomInfOper
{
/// <summary>
/// 添加客户信息
/// </summary>
/// <param name="info"></param>
public void AddCustomInf(CustomInf info)
{
}
}
http://blog.csdn.net/zjlme
- 以XML为数据传输格式的Web service设计方案示列
- web应用的数据传输格式
- 以服务为中心的网络之web service
- jQuery以json格式调用 Web Service
- 万能的数据传输格式——XML
- Web Service压缩数据传输
- 数据传输格式xml/json
- web数据传输只有两种格式:json和xml
- web service的api说明文档xml格式 - WSDL(Web Service Description Language)
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- 提高 Web Service 数据传输效率的基本方法
- Android网络通信中数据传输的XML格式使用
- 万能的数据传输格式——XML/json
- 部分JS
- 一些色彩值的转换
- JSON用法
- C#导入Excel方法之一
- vs 2005 断点失灵问题
- 以XML为数据传输格式的Web service设计方案示列
- 嵌入式linux启动信息完全注释
- JSON用法2
- 重载操作符与转换(1)
- structs中的action类
- 栈,堆,堆栈
- 重载操作符(2)
- 使用Silverlight2dataGrid例子(完整XAML)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(前言)