Web Service 代理类组成

客户端调用Web Service的方式我现在知道的有三种,分别为Http_Get,Http_Post和通过代理类来调用
直接通过HTTP-GET和直接通过HTTP-POST来请求访问Web服务是非常底层的且麻烦,(详细用法请查看C#分类中的说明),还有一种就是通过代理类来访问,DoNet框架提供的WSDL.EXE可以产生要求的代理类.

在.net命令里输入wsdl http//url/xxx.asmx /language:cs /out:xxx.ss /protocol:httpGet即可生成HttpGet的代理类

首先我将列出WebService的原代码:

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

namespace Play.CH1
{
 public class Service1 : System.Web.Services.WebService
 {
  public Service1()
  {
   InitializeComponent();
  }

  private System.Data.SqlClient.SqlConnection sqlConnection1;
 
  private IContainer components = null;
    
  private void InitializeComponent()
  {
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

   this.sqlConnection1.ConnectionString = "workstation id=\"192.168.1.9\";packet size=4096;user id=sa;data source=\"192.168.1.9\";pe" +
    "rsist security info=True;initial catalog=pubs;password=";
   this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);

  }
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
  
  }

  [WebMethod]
  public string doSearch(string keyword)
  {
   SqlDataAdapter da = new  SqlDataAdapter("select top 20 title_id,title from titles where title like @title",sqlConnection1);
   da.SelectCommand.Parameters.Add(new SqlParameter("@title","%"+keyword+"%"));
   DataSet ds=new DataSet();
   da.Fill(ds);
   return ds.GetXml();
  }

 }
}

再贴出刚生所生成的代理类的原代码
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// 這個原始程式碼是由 wsdl,Version=1.1.4322.573 自動產生。
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public class Service1 : System.Web.Services.Protocols.HttpGetClientProtocol {
   
    public Service1() {
        this.Url = "
http://localhost/Play/CH1/Service1.asmx";
    }
   
    [System.Web.Services.Protocols.HttpMethodAttribute(typeof(System.Web.Services.Protocols.XmlReturnReader), typeof(System.Web.Services.Protocols.UrlParameterWriter))]
    [return: System.Xml.Serialization.XmlRootAttribute("string", Namespace="
http://tempuri.org/", IsNullable=true)]
    public string doSearch(string keyword) {
        return ((string)(this.Invoke("doSearch", (this.Url + "/doSearch"), new object[] {
                    keyword})));
    }
   
    public System.IAsyncResult BegindoSearch(string keyword, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("doSearch", (this.Url + "/doSearch"), new object[] {
                    keyword}, callback, asyncState);
    }

    public string EnddoSearch(System.IAsyncResult asyncResult) {
        return ((string)(this.EndInvoke(asyncResult)));
    }
}

再来对代理类进行详细说明

1. 代理类开始是引出一系列的命名空间,代码的主题是定义一个跟待访问的Web服务类同名的 从System.Web.Services.Protocols.HttpGetClientProtocol派生的代理类
2. 代理类具有如下四个方法
2.1 不带参数的构造函数
构造函数主要是设定代理类的URL属性,这个属性表示WEB服务的URL.
2.2 doSearch方法
该方法是WEB服务的doSearch方法的本地代理版本,该方法被一个HttpMethodAttribute所标注,其目的是指示如何序列化发送到WEB服务的doSearch方法的参数以及如何反序列化WEB服务的响应.如果采用HTTP-GET调用WEB服务的方法,则必须把HTTPMothodAttribute的ReturnFormatter属性设置为XmlReturnReader,ParameterFormatter属性设置为UrlParameterWriter.

方法的返回值也被一个XmlRootAttribute特性所标记.该特性用以将类,结构,枚举或结口标记为XML文档实例的根(或项级)元素.该特性的ElementName属性表示返回值对应的XML元素的名称,Namespace属性表示XML根元素的命名空间,IsNullable属性表示返回值为空时ElementName所指的无素是否包xsi:nil属性.
以上特性其实代替了我在C#分类中所讲的利用HTTP-GGET;HTTP-POST来获得WEB服务中的设置URL,在URL中附中参数,以及从返回XML文档中提结果的大段代码
再来看看方法中的代码
 return ((string)(this.Invoke("doSearch", (this.Url + "/doSearch"), new object[] { keyword})));
上述代码先调用一个Invoke方法,(该方法继承自父类HttpGetClientProtocol),然后把调用结果强制转化为字符串类型.
Invoke方法的原型:
protected Object Invoke(string MethodName,string RequestUrl,Object[] Parameters);
参数:MethodName->表示要调用的Web服务的名称,本例中的“
doSearch“
参数:RequestUrl->表示WEB服务的方法的URL,它是由WEB服务的URL加上"/方法名"组成
参数:Parameters->是一个对象数组,包含要传递到远程WEB服务的参数,数组中的顺序与派生类的调用方法中的参数顺序对应
返回值包含派生类方法的返回值和所有引用或输出参数的对象数组
2.3 BegindoSearch方法
这个方法用以启动对WEB服务的doSearch方法的异步调用
BegindoSearch的参数:
    keyword:    参数.
    callback:     AsyncCallback类型的委托对象,当异步调用结束后,callback所代表的函数将被回调,用来接收结果.
                      AsyncCallback委托原型:
                        [Serializable]
                        public delegate void AsyncCallback(IAsyncResult ar);
                        其: IAsyncResult接口:用于监视和管理异步操作.
                        IAsyncResult接口的原型
                        public interface IAsyncResult
                        {
                            object AsyncState{get;}//返回被作为最后一个参数提供的对象
                            WaitHandle AsyncWaitHandle{get;}//返回WaitHandle,它可用于执行WaitHandle.WaitOne,WaitAny,WaitAll,以实现同步
                            bool CompletedSynchronously{get;}//如果开始操作同步完成,则CompletedSynchronously=true
                            bool IsCompleted{get;}//在服务器完成调用处理后IsCompleted=true
                        }


2.4 EnddoSearch方法
这个方法用以结束异步调用,并获得调用结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值