ASP.NET創(chuàng)建Web服務(wù)之聲明
2024-07-10 12:56:03
供稿:網(wǎng)友
聲明一個(gè)xml web服務(wù)
當(dāng)你使用asp.net創(chuàng)建一個(gè)xml web服務(wù)時(shí),你要把所需的@_webservice指令放到.asmx文件的最頂端。.asmx文件的存在和@_ webservice指令與所實(shí)現(xiàn)的xml web服務(wù)的url地址相關(guān)。接下來,你實(shí)現(xiàn)xml web服務(wù)類來定義xml web服務(wù)客戶端可見的方法和數(shù)據(jù)類型。最后,你把你的xml web服務(wù)邏輯添加到那些方法上,以便處理xml web服務(wù)請(qǐng)求和返回的響應(yīng)。你定義的xml web服務(wù)類可以直接被包含在.asmx文件中,也可以包含在一個(gè)單獨(dú)的文件中。如果你使用一個(gè)獨(dú)立的文件,它必須被編譯為二進(jìn)制碼。你可以選擇應(yīng)用一個(gè)webservice屬性到這個(gè)類上,來實(shí)現(xiàn)xml web服務(wù)。實(shí)現(xiàn)xml web服務(wù)的類可以繼承于webservice類。
聲明一個(gè)xml web服務(wù),這個(gè)服務(wù)的實(shí)現(xiàn)存在于同一個(gè)文件。
添加一個(gè)@_webservice指令到一個(gè).asmx文件的頂部,規(guī)定在這個(gè)實(shí)現(xiàn)中實(shí)現(xiàn)xml web服務(wù)的類和程序語(yǔ)言。
class屬性可以被設(shè)置為和@_webservice指令同一個(gè)匯編文件,或者是單獨(dú)匯編文件中的一個(gè)類。如果這個(gè)類存在于一個(gè)獨(dú)立匯編文件中,它必須放在xml web服務(wù)所在的web應(yīng)用程序目錄下的/bin目錄中。language屬性可以設(shè)置為c#、vb和js,分別表示c#、visual basic.net和jscript.net。
下列代碼示例設(shè)置@_ webservice指令的language屬性為c#,class屬性為mymath,保存在同一個(gè)文件中。
<%@ webservice language="c#" class="mymath" %>
using system.web.services;
public class mymath {
[ webmethod ]
public int add(int num1, int num2) {
return num1+num2;
}
}
聲明一個(gè)xml web服務(wù),這個(gè)服務(wù)的實(shí)現(xiàn)存在于一個(gè)匯編文件中。
添加一個(gè)@_webservice指令到一個(gè).asmx文件的頂部,規(guī)定實(shí)現(xiàn)xml web服務(wù)的類,匯編文件中包含的實(shí)現(xiàn)和在這個(gè)實(shí)現(xiàn)中使用的程序語(yǔ)言。
下面的@_webservice指令是一個(gè).asmx文件中唯一的一行代碼,指定了myname.mywebservice類存在于xml web服務(wù)的web應(yīng)用程序下/bin目錄中的myassembly匯編文件中。
[c#]
<%@ webservice language="c#" class="myname.mywebservice,myassembly" %>
[visual basic]
<%@ webservice language="vb" class="myname.mywebservice,myassembly" %>
聲明一個(gè)xml web服務(wù),這個(gè)服務(wù)的實(shí)現(xiàn)存在于一個(gè)匯編文件中。
添加一個(gè)@_webservice指令到一個(gè).asmx文件的頂部,規(guī)定實(shí)現(xiàn)xml web服務(wù)的類,匯編文件中包含的實(shí)現(xiàn)和在這個(gè)實(shí)現(xiàn)中使用的程序語(yǔ)言。
下面的@_webservice指令是一個(gè).asmx文件中唯一的一行代碼,指定了myname.mywebservice類存在于xml web服務(wù)的web應(yīng)用程序下/bin目錄中的myassembly匯編文件中。
注意:如果你不在@_ webservice指令中指定一個(gè)匯編,那么asp.net在xml web服務(wù)第一次被訪問的時(shí)候搜尋存放xml web服務(wù)的web應(yīng)用程序的/bin目錄下匯編文件的列表。所以,如果你提供匯編文件名,你將改善第一次訪問時(shí)的系統(tǒng)性能。
應(yīng)用webservice屬性
通過應(yīng)用可選的webservice屬性到實(shí)現(xiàn)一個(gè)xml web服務(wù)的類上,你可以使用一個(gè)描述xml web服務(wù)的字符串來設(shè)置這個(gè)xml web服務(wù)的默認(rèn)xml域名空間。
強(qiáng)烈建議這個(gè)默認(rèn)域名空間(此處為http://tempuri.org)在xml web服務(wù)公開使用前被修改。這是很重要的,因?yàn)槟愕膞ml web服務(wù)必須和其他的無(wú)意中使用默認(rèn)值作為域名空間的xml web服務(wù)區(qū)分開來。
設(shè)置成員xml web服務(wù)的xml域名空間
應(yīng)用一個(gè)webservice屬性到實(shí)現(xiàn)xml web服務(wù)的類,設(shè)置namespace屬性。
下面的代碼示例設(shè)置xml域名空間為http://www.contoso.com/。
[c#]
<%@ webservice language="c#" class="math" debug=true%>
using system.web.services;
using system;
[webservice(namespace="http://www.contoso.com/")]
public class math {
[ webmethod ]
public int add(int num1, int num2) {
return num1+num2;
}
}
[visual basic]
<%@ webservice language="vb" class="math"%>
imports system.web.services
imports system
<webservice(namespace:="http://www.contoso.com/")> _
public class math
<webmethod()> public function add(num1 as integer, num2 as integer) as integer
return num1 + num2
end function
end class
從webservice類衍生
使用asp.net創(chuàng)建的實(shí)現(xiàn)一個(gè)xml web服務(wù)的類可以選擇性地衍生于webservice類來獲得訪問公共的asp.net對(duì)象,例如application、session、user和context的權(quán)限。application和session屬性提供保存和接收web應(yīng)用程序的生命周期或一個(gè)特定的會(huì)話的狀態(tài)的權(quán)限。想獲得關(guān)于狀態(tài)的更多的信息,請(qǐng)看在使用asp.net創(chuàng)建的xml web服務(wù)中管理狀態(tài)一節(jié)。user屬性包含了xml web服務(wù)調(diào)用者的身份。xml web服務(wù)可以使用調(diào)用者身份來判定請(qǐng)求是否被授權(quán)。有關(guān)驗(yàn)證的更多信息,請(qǐng)看加強(qiáng)xml web服務(wù)安全一節(jié)。context屬性提供了取得xml web服務(wù)客戶端請(qǐng)求的所有特定http信息的權(quán)限。
下面的代碼示例使用context屬性來獲得服務(wù)器上的請(qǐng)求時(shí)間。
[c#]
<%@ webservice language="c#" class="util" %>
using system;
using system.web.services;
public class util: webservice {
[ webmethod(description="returns the time as stored on the server",enablesession=false)]
public string time()
{
return context.timestamp.timeofday.tostring();
}
}
[visual basic]
<%@ webservice language="vb" class="util" %>
imports system
imports system.web.services
public class util
inherits webservice
<webmethod(description := "returns the time as stored on the server", _
enablesession := false)> _
public function time() as string
return context.timestamp.timeofday.tostring()
end function
end class
定義xml web服務(wù)方法
用來實(shí)現(xiàn)xml web服務(wù)的類的方法不能自動(dòng)通過web與之通訊,但是有了使用asp.net創(chuàng)建的xml web服務(wù),就能夠很容易的天家這種能力。為了添加這種功能,需要應(yīng)用一個(gè)webmethod屬性到公共方法中。能夠通過web與之通訊的xml web服務(wù)的方法被稱為xml web服務(wù)方法。
xml web服務(wù)方法是xml web服務(wù)使用的消息傳遞基礎(chǔ)結(jié)構(gòu)的關(guān)鍵組成部分。說得更精確些,一個(gè)客戶端和一個(gè)xml web服務(wù)使用消息,尤其是soap消息進(jìn)行通訊。客戶端發(fā)送一個(gè)soap請(qǐng)求到xml web服務(wù)中,而一個(gè)xml web服務(wù)方法返回一個(gè)soap響應(yīng)。xml web服務(wù)定義了它使用操作接受的消息類型,正如web服務(wù)描述語(yǔ)言中定義的那樣。這些操作與一個(gè)xml web服務(wù)中的每個(gè)xml web服務(wù)方法關(guān)聯(lián)。 即使這些xml web服務(wù)方法中的每一個(gè)都是在asp.net使用一個(gè)類的方法定義的,但要實(shí)現(xiàn)通過網(wǎng)絡(luò)傳送的數(shù)據(jù),必須把數(shù)據(jù)序列化為xml。同樣地,重要的是要記得xml web服務(wù)并不能取代dcom,我們應(yīng)該說xml web服務(wù)是跨越使用行業(yè)標(biāo)準(zhǔn)的平臺(tái)通信的一種消息傳遞基礎(chǔ)結(jié)構(gòu)。
聲明一個(gè)xml web服務(wù)方法
聲明一個(gè)xml web服務(wù),添加@_webservice指令。更多信息,請(qǐng)看聲明一個(gè)xml web服務(wù)一節(jié)。
添加公共方法到實(shí)現(xiàn)xml web服務(wù)的類中。
應(yīng)用webmethod屬性到你想要映射到操作的公共方法。
下面的代碼示例有兩個(gè)公共方法,其一是一個(gè)xml web服務(wù)方法。multiply方法是一個(gè)xml web服務(wù)方法,因?yàn)樗幸粋€(gè)應(yīng)用到它上的webmethod屬性。
[c#]
<%@ webservice language="c#" class="util" %>
using system;
using system.web.services;
public class util: webservice
{
public int add(int a, int b)
{
return a + b;
}
[ webmethod]
public long multiply(int a, int b)
{
return a * b;
}
}
[visual basic]
<%@ webservice language="vb" class="util" %>
imports system
imports system.web.services
public class util
inherits webservice
public function add(a as integer, b as integer) as integer
return a + b
end function
< webmethod()> _
public function multiply(a as integer, b as integer) as long
return a * b
end function
end class