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的示例程序源代码:

构建WebService常用特性[含简单示例程序]using System;
构建WebService常用特性[含简单示例程序]
using System.Collections;
构建WebService常用特性[含简单示例程序]
using System.ComponentModel;
构建WebService常用特性[含简单示例程序]
using System.Data;
构建WebService常用特性[含简单示例程序]
using System.Diagnostics;
构建WebService常用特性[含简单示例程序]
using System.Web;
构建WebService常用特性[含简单示例程序]
using System.Web.Services;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]
namespace WebServiceForStock
{

构建WebService常用特性[含简单示例程序]    
/// Service1 的摘要说明。
构建WebService常用特性[含简单示例程序]    
/// </summary>
构建WebService常用特性[含简单示例程序]    public class Service1 : System.Web.Services.WebService
{
构建WebService常用特性[含简单示例程序]        
public Service1()
{
构建WebService常用特性[含简单示例程序]            
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
构建WebService常用特性[含简单示例程序]
            InitializeComponent();
构建WebService常用特性[含简单示例程序]        }

构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        
private System.Windows.Forms.TextBox textBox1;
构建WebService常用特性[含简单示例程序]

构建WebService常用特性[含简单示例程序]        
构建WebService常用特性[含简单示例程序]        
//Web 服务设计器所必需的
构建WebService常用特性[含简单示例程序]
        private IContainer components = null;
构建WebService常用特性[含简单示例程序]                

构建WebService常用特性[含简单示例程序]        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
构建WebService常用特性[含简单示例程序]        
/// 此方法的内容。
构建WebService常用特性[含简单示例程序]        
/// </summary>
构建WebService常用特性[含简单示例程序]        private void InitializeComponent()
{
构建WebService常用特性[含简单示例程序]            
this.textBox1 = new System.Windows.Forms.TextBox();
构建WebService常用特性[含简单示例程序]            
// 
构建WebService常用特性[含简单示例程序]            
// textBox1
构建WebService常用特性[含简单示例程序]            
// 
构建WebService常用特性[含简单示例程序]
            this.textBox1.Location = new System.Drawing.Point(5218);
构建WebService常用特性[含简单示例程序]            
this.textBox1.Name = "textBox1";
构建WebService常用特性[含简单示例程序]            
this.textBox1.Size = new System.Drawing.Size(20021);
构建WebService常用特性[含简单示例程序]            
this.textBox1.TabIndex = 0;
构建WebService常用特性[含简单示例程序]            
this.textBox1.Text = "";
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        }

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

构建WebService常用特性[含简单示例程序]        
/// 清理所有正在使用的资源。
构建WebService常用特性[含简单示例程序]        
/// </summary>
构建WebService常用特性[含简单示例程序]        protected override void Dispose( bool disposing )
{
构建WebService常用特性[含简单示例程序]            
if(disposing && components != null)
{
构建WebService常用特性[含简单示例程序]                components.Dispose();
构建WebService常用特性[含简单示例程序]            }

构建WebService常用特性[含简单示例程序]            
base.Dispose(disposing);        
构建WebService常用特性[含简单示例程序]        }

构建WebService常用特性[含简单示例程序]        
构建WebService常用特性[含简单示例程序]        
#endregion
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        
// WEB 服务示例
构建WebService常用特性[含简单示例程序]        
// HelloWorld() 示例服务返回字符串 Hello World
构建WebService常用特性[含简单示例程序]        
// 若要生成,请取消注释下列行,然后保存并生成项目
构建WebService常用特性[含简单示例程序]        
// 若要测试此 Web 服务,请按 F5 键
构建WebService常用特性[含简单示例程序]

构建WebService常用特性[含简单示例程序]        [WebMethod]
构建WebService常用特性[含简单示例程序]        
public User StockService(int UserID)
{
构建WebService常用特性[含简单示例程序]            User newUser
=new User();
构建WebService常用特性[含简单示例程序]            newUser.personInstance
=new Person();
构建WebService常用特性[含简单示例程序]            newUser.personInstance.Name
="Slashout";
构建WebService常用特性[含简单示例程序]            newUser.personInstance.Age
=25;
构建WebService常用特性[含简单示例程序]            newUser.Email
="slashout@163.com";
构建WebService常用特性[含简单示例程序]            newUser.pwd
="test";
构建WebService常用特性[含简单示例程序]            
return newUser;
构建WebService常用特性[含简单示例程序]        }

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

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

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


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

构建WebService常用特性[含简单示例程序]using System;
构建WebService常用特性[含简单示例程序]
using System.Web.Services;
构建WebService常用特性[含简单示例程序]
using System.Xml;
构建WebService常用特性[含简单示例程序]
using System.Xml.Serialization;
构建WebService常用特性[含简单示例程序]
using System.Xml.Schema;
构建WebService常用特性[含简单示例程序]
using System.Collections;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]
namespace WebServiceForStock
{
构建WebService常用特性[含简单示例程序]    
public class Person
{
构建WebService常用特性[含简单示例程序]        
public string Name;
构建WebService常用特性[含简单示例程序]        
public int Age;
构建WebService常用特性[含简单示例程序]    }

构建WebService常用特性[含简单示例程序]    
public class User
{
构建WebService常用特性[含简单示例程序]        
public Person personInstance ;
构建WebService常用特性[含简单示例程序]        
public string Email;
构建WebService常用特性[含简单示例程序]        
public string pwd;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]    }

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

构建WebService常用特性[含简单示例程序]    
/// CustomizeClass 的摘要说明。
构建WebService常用特性[含简单示例程序]    
/// 自定义类用XML序列化
构建WebService常用特性[含简单示例程序]    
/// 可以返回复合的类
构建WebService常用特性[含简单示例程序]    
/// </summary>
构建WebService常用特性[含简单示例程序]    public class CustomizeClass
{
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        [XmlAttribute()]
public int orderID;
构建WebService常用特性[含简单示例程序]        
public DateTime orderTime;
构建WebService常用特性[含简单示例程序]        [XmlElement(
"DateTimeRequired")]public DateTime requiredDate;
构建WebService常用特性[含简单示例程序]        
public DateTime shippedDate;
构建WebService常用特性[含简单示例程序]        
public ArrayList Details;
构建WebService常用特性[含简单示例程序]        [XmlIgnore]
public string SalesPersonID;
构建WebService常用特性[含简单示例程序]        
public CustomizeClass()
{
构建WebService常用特性[含简单示例程序]            
//
构建WebService常用特性[含简单示例程序]            
// TODO: 在此处添加构造函数逻辑
构建WebService常用特性[含简单示例程序]            
//
构建WebService常用特性[含简单示例程序]
        }        
构建WebService常用特性[含简单示例程序]    }

构建WebService常用特性[含简单示例程序]    
构建WebService常用特性[含简单示例程序]    [XmlRoot(Namespace 
= "http://www.cohowinery.com")]
构建WebService常用特性[含简单示例程序]    
public class Group
{
构建WebService常用特性[含简单示例程序]        
public string GroupName;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        
// This is for serializing Employee elements.
构建WebService常用特性[含简单示例程序]
        [XmlAnyElement(Name = "Employee")]
构建WebService常用特性[含简单示例程序]        
public XmlElement[] UnknownEmployees;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        
// This is for serializing City elements.   
构建WebService常用特性[含简单示例程序]
        [XmlAnyElement
构建WebService常用特性[含简单示例程序]             (Name 
= "City"
构建WebService常用特性[含简单示例程序]             Namespace 
= "http://www.cpandl.com")]
构建WebService常用特性[含简单示例程序]        
public XmlElement[] UnknownCity;
构建WebService常用特性[含简单示例程序]
构建WebService常用特性[含简单示例程序]        
// This one is for all other unknown elements.
构建WebService常用特性[含简单示例程序]
        [XmlAnyElement]
构建WebService常用特性[含简单示例程序]        
public XmlElement[] UnknownElements;
构建WebService常用特性[含简单示例程序]    }

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

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


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

构建WebService常用特性[含简单示例程序]  <?xml version="1.0" encoding="utf-8" ?> 
构建WebService常用特性[含简单示例程序]
<User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
构建WebService常用特性[含简单示例程序]
<personInstance>
构建WebService常用特性[含简单示例程序]  
<Name>Slashout</Name> 
构建WebService常用特性[含简单示例程序]  
<Age>25</Age> 
构建WebService常用特性[含简单示例程序]  
</personInstance>
构建WebService常用特性[含简单示例程序]  
<Email>slashout@163.com</Email> 
构建WebService常用特性[含简单示例程序]  
<pwd>test</pwd> 
构建WebService常用特性[含简单示例程序]  
</User>
假设这里是一个通用的注册用户程序,那么我们就可以通过UDDI调用这个注册方法得到我们对于用户所得到的通用注册信息,只要调用这个WebService即可了,具有平台无关性的特点

相关文章:

  • 2022-12-23
  • 2021-08-02
  • 2021-05-05
  • 2021-05-16
  • 2022-02-17
  • 2021-06-22
  • 2021-08-01
猜你喜欢
  • 2021-09-04
  • 2022-01-25
  • 2022-01-08
  • 2021-11-21
  • 2021-09-21
  • 2022-02-21
相关资源
相似解决方案