如何使用C#創(chuàng)建WebService
C#學(xué)習(xí)到一定程度會(huì)涉及到C#創(chuàng)建WebService的一些高階知識(shí),本文試圖對(duì)此做一個(gè)簡(jiǎn)單的介紹。
假設(shè)A是客戶端,B是webservice服務(wù)端,用戶通過(guò)http協(xié)議向服務(wù)器發(fā)送soap請(qǐng)求,webservice返回客戶端Xml格式的數(shù)據(jù)。
現(xiàn)在我們看一看創(chuàng)建一個(gè)C#創(chuàng)建WebService的大致過(guò)程:
服務(wù)端的webservice是必須要建的。中間的soap,Xml我們不用去關(guān)心,在客戶端這邊,比較重要的是如何從webservice取得對(duì)象?答案是用的是proxy對(duì)象??蛻舳擞纱韺?duì)象(proxy)負(fù)責(zé)與webservice的通信。所以在客戶端使用webservice,完全和使用一個(gè)本地對(duì)象是一樣的。
我們現(xiàn)在以一個(gè)簡(jiǎn)單的C#創(chuàng)建WebService實(shí)例來(lái)說(shuō)明。
打開vs.Net,新建工程(asp.Net web服務(wù)),在位置中鍵入http://localhost/webserver,其中webserver就是工程的名字。確定后,出現(xiàn)一個(gè)Service1.asmx.cx,雙擊,出現(xiàn)代碼窗口,
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Web;
- using System.Web.Services;
- namespace webserver
- {
- ///
- /// Service1 的摘要說(shuō)明。
- ///
- (1)
- public class Service1 :
- System.Web.Services.WebService
- {
- public Service1()
- {
- //CODEGEN:該調(diào)用是 ASP.Net Web
- 服務(wù)設(shè)計(jì)器所必需的
- InitializeComponent();
- }
- #region Component Designer generated code
- //Web 服務(wù)設(shè)計(jì)器所必需的
- private IContainer components = null;
- ///
- /// 設(shè)計(jì)器支持所需的方法 -
- 不要使用代碼編輯器修改
- /// 此方法的內(nèi)容。
- ///
- private void InitializeComponent()
- {
- }
- ///
- /// 清理所有正在使用的資源。
- ///
- protected override void Dispose
- ( bool disposing )
- {
- if(disposing && components != null)
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #endregion
- // WEB 服務(wù)示例
- // HelloWorld() 示例服務(wù)返回字符串 Hello World
- // 若要生成,請(qǐng)取消注釋下列行,然后保存并生成項(xiàng)目
- // 若要測(cè)試此 Web 服務(wù),請(qǐng)按 F5 鍵
- // [WebMethod]
- // public string HelloWorld()
- // {
- // return "Hello World";
- // }
- }
- }
下面在(1)處加入
[WebService(Namespace="http://localhost/webserver/")]
這是因?yàn)閟oap是基于http協(xié)議上的,客戶端無(wú)法知道webservice位于那個(gè)服務(wù)器上。在實(shí)際應(yīng)用中,比如http://www.ourfly.com上放置這個(gè)webservice,則Namespace改為http://www.ourfly.com/webserver.
下面我們給這個(gè)webservice添加一個(gè)方法。
- // [WebMethod]
- // public string HelloWorld()
- // {
- // return "Hello World";
- // }
- 微軟幫我們寫好了一個(gè),接著添加一個(gè)方法。
- 方法名稱叫show.
- [WebMethod]
- public string show(string yourname)
- {
- return “http://www.ourfly.com”+”歡迎”+yourname;
- }
- 現(xiàn)在,就可以運(yùn)行了,按F5,點(diǎn)擊show,輸入你的名字,
- 然后點(diǎn)擊invote 看到了吧。
- 〈 ?Xml version="1.0" encoding="utf-8" ?〉
- 〈 string Xmlns="http://tempuri.org/"〉
- http://www.ourfly.com歡迎yyg〈 /string〉
成功了。打開bin目錄,Vs.Net已經(jīng)將proxy做好了.webserver.dll.
現(xiàn)在我們?cè)诓煌沫h(huán)境下測(cè)試:
1.打開vs.Net,新建”windows應(yīng)用程序”工程,命名為Client,增加按鈕,文本框。
現(xiàn)在要用到代理了,右鍵單擊右邊的reference(引用),選擇”添加引用”,選擇瀏覽,找到webserver目錄下的bin目錄下的webserver.dll
再加入一個(gè)system.web.webservices的引用,在列表中有。
在form1.cs里,加入
using System.Web.Services;
using webserver;
然后在
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
后面,插入
private webserver.service1 Client
建立一個(gè)service1的實(shí)例。雙擊按鈕,代碼如下:
- private void button1_Click
- (object sender, System.EventArgs e)
- {
- Client =new Service1();
- string name;
- name=Client.show("龍卷風(fēng).Net");
- textBox1.Text=name;
- }
按F5,運(yùn)行工程,點(diǎn)擊按鈕,文本框中顯示 http://www.ourfly.com歡迎龍卷風(fēng).Net
2. Asp.Net web窗口的測(cè)試
方法與上面的一模一樣,添加引用,建立service1的實(shí)例 在此不在細(xì)說(shuō)。
3.在VB中測(cè)試
這個(gè)就要相對(duì)來(lái)說(shuō)復(fù)雜一些 ,首先在VB中建立一個(gè)”標(biāo)準(zhǔn)EXE”的工程。添加引用:Microsoft Soap Type library。
注意:如果沒(méi)有安裝Microsoft Soap Toolkit,是沒(méi)有這個(gè)類型庫(kù)的。 可以在http://www.ourfly.com中下載。
- 添加一個(gè)text
- Private Sub Form_Load()
- Text1.Text = add()
- End Sub
- Public Function Add() As String
- Dim objSoapClient As New
- SoapClient objSoapClient.
- ClientProperty("ServerHTTPRequest") = True
- Call objSoapClient.mssoapinit(
- "http://localhost/webserver/service1.asmx?WSDL",
- "Service1", "Service1Soap")
- 這句也可以
- objSoapClient.mssoapinit(
- "http://localhost/webserver/service1.asmx?WSDL")
- Add = objSoapClient.Show("龍卷風(fēng).Net")
- End Function
調(diào)試成功需要注意的:
運(yùn)行服務(wù)端webservice的程序,出現(xiàn)下面時(shí) 支持下列操作。有關(guān)正式定義。
點(diǎn)擊服務(wù)說(shuō)明,會(huì)得到完整的wsdl文件 http://localhost/webserver/Service1.asmx?WSDL
我們就要使用這個(gè)文件,其中包含了我們定義的方法等等。Mssoapinit(bstrWSDLFile as string,[bStrServiceName as string ],[bStrport as string ] ,[bstrWSMLDile as string])的用法: 其中第二個(gè),第三個(gè)參數(shù)在wsdl文件中可以找到。也可以省略。
這就是C#創(chuàng)建WebService的簡(jiǎn)單過(guò)程,希望對(duì)您有所幫助。
【編輯推薦】