使用C#創建webservice及三種調用方式
GB2312?
http://csharp.xdowns.com/trackback.php?id=299&encode=gb2312
UTF-8?
http://csharp.xdowns.com/trackback.php?id=299&encode=utf-8
微軟.NET戰略的一個比較重要的部分就是webservice,利用webservice我們可以創建真正有效的分布式應用程序。?
下面,我們對webservice做一些說明。?
假設A是客戶端,B是webservice服務端,用戶通過http協議向服務器發送soap請求,webservice返回客戶端XML格式的數據。?
現在我們看一看創建一個webservice的大致過程:?
服務端的webservice是必須要建的。中間的soap,xml我們不用去關心,在客戶端這邊,比較重要的是如何從webservice取得對象?答案是用的是proxy對象。客戶端由代理對象(proxy)負責與webservice的通信。所以在客戶端使用webservice,完全和使用一個本地對象是一樣的。?
我們現在以一個簡單的實例來說明。?
打開vs.net,新建工程(asp.net web服務),在位置中鍵入
http://localhost/webserver
,其中webserver就是工程的名字。確定后,出現一個Service1.asmx.cx,雙擊,出現代碼窗口,?
using System;?
using System.Collections;?
using System.ComponentModel;?
using System.Data;?
using System.Diagnostics;?
using System.Web;?
using System.Web.Services;?
namespace webserver?
{?
/// <summary>?
/// Service1 的摘要說明。?
/// </summary>?
(1)?
public class Service1 : System.Web.Services.WebService?
{?
public Service1()?
{?
//CODEGEN:該調用是 ASP.NET Web 服務設計器所必需的?
InitializeComponent();?
}?
#region Component Designer generated code?
//Web 服務設計器所必需的?
private IContainer components = null;?
/// <summary>?
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改?
/// 此方法的內容。?
/// </summary>?
private void InitializeComponent()?
{?
}?
/// <summary>?
/// 清理所有正在使用的資源。?
/// </summary>?
protected override void Dispose( bool disposing )?
{?
if(disposing && components != null)?
{?
components.Dispose();?
}?
base.Dispose(disposing);?
}?
#endregion?
// WEB 服務示例?
// HelloWorld() 示例服務返回字符串 Hello World?
// 若要生成,請取消注釋下列行,然后保存并生成項目?
// 若要測試此 Web 服務,請按 F5 鍵?
// [WebMethod]?
// public string HelloWorld()?
// {?
// return "Hello World";?
// }?
}?
}?
下面在(1)處加入?
[WebService(Namespace="http://localhost/webserver/"]?
這是因為soap是基于http協議上的,客戶端無法知道webservice位于那個服務器上。在實際應用中,比如
http://www.ourfly.com
上放置這個webservice,則Namespace改為
http://www.ourfly.com/webserver.
?
下面我們給這個webservice添加一個方法。?
// [WebMethod]?
// public string HelloWorld()?
// {?
// return "Hello World";?
// }?
微軟幫我們寫好了一個,接著添加一個方法。方法名稱叫show.?
[WebMethod]?
public string show(string yourname)?
{?
return “
http://www.ourfly.com
”+”歡迎”+yourname;?
}?
現在,就可以運行了,按F5,點擊show,輸入你的名字,然后點擊invote?
看到了吧。?
<?xml version="1.0" encoding="utf-8" ?>?
<string xmlns="http://tempuri.org/">
http://www.ourfly.com
歡迎yyg</string>?
成功了。打開bin目錄,Vs.net已經將proxy做好了.webserver.dll.?
現在我們在不同的環境下測試:?
1. 打開vs.net,新建”windows應用程序”工程,命名為Client,增加按鈕,文本框。?
現在要用到代理了,右鍵單擊右邊的reference(引用),選擇”添加引用”,選擇瀏覽,找到webserver目錄下的bin目錄下的webserver.dll?
再加入一個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?
建立一個service1的實例。雙擊按鈕,代碼如下:?
private void button1_Click(object sender, System.EventArgs e)?
{?
Client =new Service1();?
string name;?
name=Client.show("龍卷風.NET";?
textBox1.Text=name;?
}?
按F5,運行工程,點擊按鈕,文本框中顯示?
http://www.ourfly.com
歡迎龍卷風.NET?
2. Asp.NET web窗口的測試?
方法與上面的一模一樣,添加引用,建立service1的實例?
在此不在細說。?
3.在VB中測試?
這個就要相對來說復雜一些?
首先在vb中建立一個”標準EXE”的工程。添加引用:Microsoft Soap Type library。注意:如果沒有安裝Microsoft Soap Toolkit,是沒有這個類型庫的。?
可以在
http://www.ourfly.com
中下載。?
添加一個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("龍卷風.NET"?
End Function?
調試成功需要注意的:?
運行服務端webservice的程序,出現下面時?
支持下列操作。有關正式定義,請查看服務說明。?
點擊服務說明,會得到完整的wsdl文件?
http://localhost/webserver/Service1.asmx?WSDL
?
我們就要使用這個文件,其中包含了我們定義的方法等等。?
Mssoapinit(bstrWSDLFile as string,[bStrServiceName as string ],[bStrport as string ] ,[bstrWSMLDile as string])的用法:?
其中第二個,第三個參數在wsdl文件中可以找到。也可以省略。?
后續:還有從COM中生成wsdl文件等等,我會陸續推出的。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
