• .net 代理类(WebService代理类的详解 )


    http://hi.baidu.com/654085966/item/53ee8c0f108ad78202ce1b1d   -----------转自

    客户端调用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方法
    这个方法用以结束异步调用,并获得调用结果
  • 相关阅读:
    C#.NET 超大文件上传和断点续传的实现
    Linux下arp用法
    daf简记
    Linux route命令详解和使用示例(查看和操作IP路由表)
    CMake使用之一
    Crontab的格式
    linux 单网卡绑定两个ip
    云计算与虚拟化以及IaaS, PaaS和SaaS
    shell中判断用法
    解决screen Cannot open your terminal '/dev/pts/1'问题
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3481857.html
Copyright © 2020-2023  润新知