將 WebMethod 屬性 (Attribute) 附加至 Public 方法,表示您要方法公開為 XML Web Service 的一部分。您也可使用這個屬性 (Attribute) 的屬性 (Property),進一步設定 XML Web Service 方法的行為。如需詳細資訊,請參閱程式碼模型:以 Managed 程式碼建立的 XML Web Service。
WebMethod 屬性 (Attribute) 提供下列屬性 (Property):
BufferResponse
WebMethod 屬性 (Attribute) 的 BufferResponse 屬性 (Property) 會啟用 XML Web Service 方法的回應緩衝。當設定為 true 時 (預設值),ASP.NET 將回應傳送至用戶端之前,會緩衝整個回應。由於緩衝使背景工作處理序與 IIS 處理序之間的通訊降至最低限度,所以緩衝非常有效率並且有助於提高效能。當設定為 false,ASP.NET 會以 16KB 區塊為單位來緩衝回應。一般而言,只有您不要將整個回應內容一次置入記憶體中時,才會將這個屬性 (Property) 設定為 false。例如,您正在回寫資料流向資料庫範圍外的項目的集合。除非以別的方式指定,否則預設值為 true。如需詳細資訊,請參閱 WebMethodAttribute.BufferResponse 屬性。
若要緩衝 XML Web Service 方法的回應
CacheDuration
WebMethod 屬性 (Attribute) 的 CacheDuration 屬性 (Property) 會啟用 XML Web Service 方法結果的快取。ASP.NET 將快取每個唯一參數集的結果。這個屬性的值會指定 ASP.NET 應快取結果的間隔秒數。零值會停用結果快取。除非以別的方式指定,否則預設值為零。如需詳細資訊,請參閱 WebMethodAttribute.CacheDuration 屬性。
若要快取 XML Web Service 方法的結果
Description
WebMethod 屬性 (Attribute) 的 Description 屬性 (Property) 提供 XML Web Service 方法的描述,此描述將顯示於服務說明網頁。除非以別的方式指定,否則預設值為空字串。如需詳細資訊,請參閱 WebMethodAttribute.Description 屬性。
若要提供 XML Web Service 方法的描述
EnableSession
WebMethod 屬性 (Attribute) 的 EnableSession 屬性 (Property) 會啟用 XML Web Service 方法的工作階段狀態。一旦啟用後,XML Web Service 就可直接從 HttpContext.Current.Session 存取工作階段狀態集合,或是如果它繼承自 WebService 基底類別,則可使用 WebService.Session 屬性 (Property)。除非以別的方式指定,否則預設值為 false。如需詳細資訊,請參閱 WebMethodAttribute.EnableSession 屬性。
若要啟用 XML Web Service 方法中的工作階段狀態
MessageName
WebMethod 屬性 (Attribute) 的 MessageName 屬性 (Property) 會讓 XML Web Service 使用別名 (Alias) 來唯一辨認多載方法。除非以別的方式指定,否則預設值為方法名稱。當指定 MessageName 時,產生的 SOAP 訊息將反映這個名稱,而不是實際的方法名稱。如需詳細資訊,請參閱 WebMethodAttribute.MessageName 屬性。
若要提供 XML Web Service 方法的訊息名稱
-
使用 WebMethod 屬性 (Attribute) 的 MessageName 屬性 (Property),如下列範例所示:
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod(MessageName:="AddDoubles")> _
Public Function Add(ByVal dValueOne As Double, _
ByVal dValueTwo As Double) As Double
Add = dValueOne + dValueTwo
End Function
<System.Web.Services.WebMethod(MessageName:="AddIntegers")> _
Public Function Add(ByVal iValueOne As Integer, _
ByVal iValueTwo As Integer) As Integer
Add = iValueOne + iValueTwo
End Function
End Class
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(MessageName="AddDoubles")]
public double Add(double dValueOne, double dValueTwo)
{
return dValueOne + dValueTwo;
}
[System.Web.Services.WebMethod(MessageName="AddIntegers")]
public int Add(int iValueOne, int iValueTwo)
{
return iValueOne + iValueTwo;
}
}
加入雙精度浮點數 (Double) AddDoubles 的方法的 SOAP 要求訊息會如下列所示:
POST /myWebService/Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AddDoubles"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddDoubles xmlns="http://tempuri.org/">
<dValueOne>double</dValueOne>
<dValueTwo>double</dValueTwo>
</AddDoubles>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length 加入雙精度浮點數 AddDoubles 的方法的 SOAP 要求訊息會如下列所示:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddDoublesResponse xmlns="http://tempuri.org/">
<AddDoublesResult>double</AddDoublesResult>
</AddDoublesResponse>
</soap:Body>
</soap:Envelope>
TransactionOption
WebMethod 屬性 (Attribute) 的 TransactionOption 屬性 (Property) 可讓 XML 服務方法加入成為異動 (Transaction) 之根物件 (Root Object) 的一部分。雖然您可將 TransactionOption 屬性 (Property) 設定為任何的 TransactionOption 列舉型別的值,但是 XML Web Service 方法只有兩種可能的行為;它不參與異動 (Disabled、NotSupported、Supported) 或建立新的異動 (Required、RequiresNew)。除非以別的方式指定,否則預設值為 TransactionOption.Disabled。如需詳細資訊,請參閱 WebMethodAttribute.TransactionOption 屬性。
除了任何 XML Web Service 方法的先決條件,您還需要加入一個參考至 System.EnterpriseServices.dll。此命名空間包含公開在 COM+ Service 中找到的分散式異動模型的方法和屬性。System.EnterpriseServices.ContextUtil 類別讓您使用 SetAbort 或 SetComplete 方法來表決異動。如需詳細資訊,請參閱參與使用 ASP.NET 建立的 XML Web Service 中的異動及自動異動和 XML Web Service。
若要使用 XML Web Service 方法來建立新的異動
-
加入參考至 System.EnterpriseServices.dll。如需詳細資訊,請參閱加入和移除參考。
-
將 System.EnterpriseServices 命名空間加入至 XML Web Service,如下列範例所示:
Imports System.EnterpriseServices
using System.EnterpriseServices;
-
使用 WebMethod 屬性 (Attribute) 的 TransactionOption 屬性 (Property),如下列範例所示:
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod( _
TransactionOption:=TransactionOption.RequiresNew)> _
Public Function DoSomethingTransactional() As String
'The transaction was successful...
ContextUtil.SetComplete
DoSomethingTransactional = ContextUtil.TransactionId.ToString()
End Function
End Class
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(
TransactionOption=TransactionOption.RequiresNew)]
public string DoSomethingTransactional()
{
// The transaction was successful...
ContextUtil.SetComplete();
return ContextUtil.TransactionId.ToString();
}
}
請參閱