以XML为数据传输格式的Web service设计方案
本文的开发环境 Vss 2008
目录
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 服务结构图
下面以添加客户信息为例 各层之间的定义
包含权限票据接口和正常的服务功能接口

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

具体的业务
添加客户信息

posted on 2008-08-25 22:51 狗尾草-大数据收割基 阅读(19035) 评论(6) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!