c# Webservice 組建代理程式類並在用戶端調用

來源:互聯網
上載者:User

產生:

VS2005命令:

開始》運行》CMD

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>wsdl /language:c# /n:XYS.MISS.Photo.PhotoAdmin /out:d:\Temp\TestService.cs D:\Temp\TestService.wsdl

在d:/Temp下就會產生一個TestService.cs 檔案

預設為C#語言/language:VB 為輸出語言/n:mynamespace namespace/out:myProxyClass.cs 輸出檔案名

注意:D:\Temp\TestService.wsdl 是wsdl路徑,可以是url路徑:http://localhost/Temp/Test.asmx?wsdl

 

VS2008命令: 

Microsfot visual studeio 2008》Visual Studio Tools》D:\Program Files\Microsoft Visual Studio 9.0\VC>

svcutil.exe http://localhost:1101/UpdateService.svc /serializer:DataContractSerializer

在D:\Program Files\Microsoft Visual Studio 9.0\VC 檔案夾下自動產生一個UpdateService.cs檔案和output.config檔案,把output.config對應結點

<system.serviceModel>下的內容copy到要調用服務的web.config下面即可

 

PS:

調用WebService很多人喜歡直接添加Web引用,事實上,如果你的項目裡只有一個Web服務那麼可能不會出現什麼問題,但是如果你的項目裡引用兩個以上的Web服務,並且這兩個服務裡可能存在相同的類名,這時候可能就會出現問題。比較保險的辦法就是用代理類的方式,把多個服務中相同的部分合并起來。

 

調用:

把TestService.cs 方法拷貝到項目中並可以調用,在Default.cs代碼中調用:

XYS.MISS.Photo.PhotoAdmin aa = new XYS.MISS.Photo.PhotoAdmin();  //根據產生代碼類時設定的名稱空間下找到類,並執行個體化
 string hospitalName=actionPhoto.GetHospitalName();         //調用webService代理類中的方法

 

說明:並且可以動態配置WebService地址,比如以下是代理類:

 public partial class WebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
       
        private System.Threading.SendOrPostCallback HelloWorldOperationCompleted;
       
        /// <remarks/>
        public WebService() {
            this.Url = Class1.websvUrl+"/WebService.asmx";   //建構函式中,這裡可以動態配置WebServiceUrl地址
        }
       
        /// <remarks/>
        public event HelloWorldCompletedEventHandler HelloWorldCompleted;
       
        /// <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string HelloWorld() {
            object[] results = this.Invoke("HelloWorld", new object[0]);
            return ((string)(results[0]));
        }

}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.