构建WebService常用特性[含简单示例程序]

WebService Method Attributes Properties

1.Infomation 属性:

Description
MessageName

2.Behavior属性:

BufferResponse

CacheDuration

EnableSession

TransactionOption

使用WebService代理类

1.Examples:IDispatch,COM interop wrappers(RCW)

2.透明的,处理底层细节

3.使用WebService API函数 inhertis System.Web.Services.Protocols.SoapHttpClientsProtocol

4.处理:
Marshalling  of Parameters

XML序列化/反序列化

SOAP加密/解密

一个WebService对应三个方法:

一个同步/两个异步

KeyProperties:
Url,TimeOut,Proxy,RequestEncoding,UserAgents,AllowAutoRedirection

返回复杂的数据类型

1>数据集
2>XML
3>二进制数据[pdf,图片]

.Net使得一切简单化了

把简单的对象和属性序列化了

下面我们来看一下通过一个WebService串行化一个XML的示例程序源代码:

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Diagnostics;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
None.gif
namespace  WebServiceForStock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Service1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Service1 : System.Web.Services.WebService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Service1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InBlock.gif
            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Windows.Forms.TextBox textBox1;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
InBlock.gif        
InBlock.gif        
//Web 服务设计器所必需的
InBlock.gif
        private IContainer components = null;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif            
// 
InBlock.gif            
// textBox1
InBlock.gif            
// 
InBlock.gif
            this.textBox1.Location = new System.Drawing.Point(5218);
InBlock.gif            
this.textBox1.Name = "textBox1";
InBlock.gif            
this.textBox1.Size = new System.Drawing.Size(20021);
InBlock.gif            
this.textBox1.TabIndex = 0;
InBlock.gif            
this.textBox1.Text = "";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(disposing && components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
// WEB 服务示例
InBlock.gif        
// HelloWorld() 示例服务返回字符串 Hello World
InBlock.gif        
// 若要生成,请取消注释下列行,然后保存并生成项目
InBlock.gif        
// 若要测试此 Web 服务,请按 F5 键
InBlock.gif

InBlock.gif        [WebMethod]
InBlock.gif        
public User StockService(int UserID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            User newUser
=new User();
InBlock.gif            newUser.personInstance
=new Person();
InBlock.gif            newUser.personInstance.Name
="Slashout";
InBlock.gif            newUser.personInstance.Age
=25;
InBlock.gif            newUser.Email
="slashout@163.com";
InBlock.gif            newUser.pwd
="test";
InBlock.gif            
return newUser;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


然后看我们实例化类的程序,我们建立一个类文件:

None.gif using  System;
None.gif
using  System.Web.Services;
None.gif
using  System.Xml;
None.gif
using  System.Xml.Serialization;
None.gif
using  System.Xml.Schema;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  WebServiceForStock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Person
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string Name;
InBlock.gif        
public int Age;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Person personInstance ;
InBlock.gif        
public string Email;
InBlock.gif        
public string pwd;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CustomizeClass 的摘要说明。
InBlock.gif    
/// 自定义类用XML序列化
InBlock.gif    
/// 可以返回复合的类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CustomizeClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        [XmlAttribute()]
public int orderID;
InBlock.gif        
public DateTime orderTime;
InBlock.gif        [XmlElement(
"DateTimeRequired")]public DateTime requiredDate;
InBlock.gif        
public DateTime shippedDate;
InBlock.gif        
public ArrayList Details;
InBlock.gif        [XmlIgnore]
public string SalesPersonID;
InBlock.gif        
public CustomizeClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }
        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    [XmlRoot(Namespace 
= "http://www.cohowinery.com")]
InBlock.gif    
public class Group
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string GroupName;
InBlock.gif
InBlock.gif        
// This is for serializing Employee elements.
InBlock.gif
        [XmlAnyElement(Name = "Employee")]
InBlock.gif        
public XmlElement[] UnknownEmployees;
InBlock.gif
InBlock.gif        
// This is for serializing City elements.   
InBlock.gif
        [XmlAnyElement
InBlock.gif             (Name 
= "City"
InBlock.gif             Namespace 
= "http://www.cpandl.com")]
InBlock.gif        
public XmlElement[] UnknownCity;
InBlock.gif
InBlock.gif        
// This one is for all other unknown elements.
InBlock.gif
        [XmlAnyElement]
InBlock.gif        
public XmlElement[] UnknownElements;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif


好了程序运行结果返回一个XML源代码:

None.gif    <? xml version="1.0" encoding="utf-8"  ?>  
None.gif
< User  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xmlns ="http://tempuri.org/" >
None.gif
< personInstance >
None.gif  
< Name > Slashout </ Name >  
None.gif  
< Age > 25 </ Age >  
None.gif  
</ personInstance >
None.gif  
< Email > slashout@163.com </ Email >  
None.gif  
< pwd > test </ pwd >  
None.gif  
</ User >
假设这里是一个通用的注册用户程序,那么我们就可以通过UDDI调用这个注册方法得到我们对于用户所得到的通用注册信息,只要调用这个WebService即可了,具有平台无关性的特点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值