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

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebServiceForStock
{
    
/// <summary>
    
/// Service1 的摘要说明。
    
/// </summary>

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


        
private System.Windows.Forms.TextBox textBox1;

        
组件设计器生成的代码

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

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

    }

}



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

using System;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections;


namespace WebServiceForStock
{
    
public class Person
    
{
        
public string Name;
        
public int Age;
    }

    
public class User
    
{
        
public Person personInstance ;
        
public string Email;
        
public string pwd;

    }


    
/// <summary>
    
/// CustomizeClass 的摘要说明。
    
/// 自定义类用XML序列化
    
/// 可以返回复合的类
    
/// </summary>

    public class CustomizeClass
    
{

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

    
    [XmlRoot(Namespace 
= "http://www.cohowinery.com")]
    
public class Group
    
{
        
public string GroupName;

        
// This is for serializing Employee elements.
        [XmlAnyElement(Name = "Employee")]
        
public XmlElement[] UnknownEmployees;

        
// This is for serializing City elements.   
        [XmlAnyElement
             (Name 
= "City"
             Namespace 
= "http://www.cpandl.com")]
        
public XmlElement[] UnknownCity;

        
// This one is for all other unknown elements.
        [XmlAnyElement]
        
public XmlElement[] UnknownElements;
    }



}



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

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

本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/03/26/126193.html 关注公众号:数字化转型

原文地址:https://www.cnblogs.com/SlashOut/p/126193.html