将 WebMethod 属性附加到 Public 方法表示希望将该方法公开为 XML Web services 的一部分。您还可以使用该特性的属性进一步配置 XML Web services 方法的行为。有关更多信息,请参见托管代码中的 XML Web services 的代码模型。
WebMethod 属性提供以下属性:
BufferResponse
CacheDuration
Description
EnableSession
MessageName
TransactionOption
BufferResponse
WebMethod 特性的 BufferResponse 属性启用对 XML Web services 方法响应的缓冲。当设置为 true(默认设置)时,ASP.NET 在将响应向下发送到客户端之前对整个响应进行缓冲。缓冲非常有效,它通过最小化辅助进程和 IIS 进程之间的通信来帮助提高性能。当设置为 false 时,ASP.NET 以 16KB 的块区缓冲响应。通常,只有在不想将响应的全部内容一次缓冲到内存时,才将该属性设置为 false。例如,您在反写一个集合,该集合正在以流的形式从数据库输出其项。除非另外指定,默认值为 true。有关更多信息,请参见 WebMethodAttribute.BufferResponse 属性。
缓冲 XML Web services 方法的响应
使用 WebMethod 属性的 BufferResponse 属性,如下所示:
' Visual Basic
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod(BufferResponse:=False)> _
Public Function GetBigData() As DataSet
'implementation code
End Function
End Class
// C#
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(BufferResponse=false)]
public DataSet GetBigData()
{
//implementation code
}
}CacheDuration
WebMethod 特性的 CacheDuration 属性启用对 XML Web services 方法结果的缓存。ASP.NET 将缓存每个唯一参数集的结果。该属性的值指定 ASP.NET 应该对结果进行多少秒的缓存处理。值为零,则禁用对结果进行缓存。除非另外指定,默认值为零。有关更多信息,请参见 WebMethodAttribute.CacheDuration 属性。
缓存 XML Web services 方法的结果
使用 WebMethod 属性的 CacheDuration 属性,如下所示:
' Visual Basic
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod(CacheDuration:=60)> _
Public Function ConvertTemperature(ByVal dFahrenheit As Double) _
As Double
ConvertTemperature = ((dFahrenheit - 32) * 5) / 9
End Function
End Class
// C#
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(CacheDuration=60)]
public double ConvertTemperature(double dFahrenheit)
{
return ((dFahrenheit - 32) * 5) / 9;
}
}Description
WebMethod 特性的 Description 属性提供 XML Web services 方法的说明,该说明将显示在服务帮助页上。除非另外指定,默认值为空字符串。有关更多信息,请参见 WebMethodAttribute.Description 属性。
提供 XML Web services 方法的说明
使用 WebMethod 属性的 Description 属性,如下所示:
' Visual Basic
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod( _
Description:="This method converts a temperature " & _
"in degrees Fahrenheit to a temperature in degrees Celsius.")> _
Public Function ConvertTemperature(ByVal dFahrenheit As Double) _
As Double
ConvertTemperature = ((dFahrenheit - 32) * 5) / 9
End Function
End Class
// C#
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(
Description="Converts F to C a temperature in " +
"degrees Fahrenheit to a temperature in degrees Celsius.")]
public double ConvertTemperature(double dFahrenheit)
{
return ((dFahrenheit - 32) * 5) / 9;
}
}EnableSession
WebMethod 特性的 EnableSession 属性启用 XML Web services 方法的会话状态。一旦启用,XML Web services 就可以从 HttpContext.Current.Session 中直接访问会话状态集合,或者,如果它是从 WebService 基类继承的,则可以使用 WebService.Session 属性来访问会话状态集合。除非另外指定,默认值为 false。有关更多信息,请参见 WebMethodAttribute.EnableSession 属性。
在 XML Web services 方法中启用会话状态
使用 WebMethod 属性的 EnableSession 属性,如下所示:
' Visual Basic
Public Class Service1
Inherits System.Web.Services.WebService
<System.Web.Services.WebMethod(EnableSession:=True)> _
Public Function ConvertTemperature(ByVal dFahrenheit As Double) _
As Double
Session("Conversions") = Session("Conversions") + 1
ConvertTemperature = ((dFahrenheit - 32) * 5) / 9
End Function
<System.Web.Services.WebMethod(EnableSession:=True)> _
Public Function GetNumberOfConversions() As Integer
GetNumberOfConversions = Session("Conversions")
End Function
End Class
// C#
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(EnableSession=true)]
public double ConvertTemperature(double dFahrenheit)
{
Session["Conversions"] = (int) Session["Conversions"] + 1;
return ((dFahrenheit - 32) * 5) / 9;
}
[System.Web.Services.WebMethod(EnableSession=true)]
public int GetNumberOfConversions()
{
return (int) Session["Conversions"];
}
}MessageName
WebMethod 特性的 MessageName 属性使 XML Web services 能够唯一确定使用别名的重载方法。除非另外指定,默认值是方法名称。当指定 MessageName 时,结果 SOAP 消息将反映该名称,而不是实际的方法名称。有关更多信息,请参见 WebMethodAttribute.MessageName 属性。
为 XML Web services 方法提供消息名
使用 WebMethod 属性的 MessageName 属性,如下所示:
' Visual Basic
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
// C#
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;
}
}添加双精度型 (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 特性的 TransactionOption 属性使 XML Web services 方法可以作为事务的根对象参与。虽然可以将 TransactionOption 属性设置为 TransactionOption 枚举的任意值,但 XML Web services 方法仅有两个可能的行为:它不参与事务(Disabled、NotSupported、Supported)或它创建一个新事务(Required、RequiresNew)。除非另外指定,默认值为 TransactionOption.Disabled。有关更多信息,请参见 WebMethodAttribute.TransactionOption 属性。
除了任何 XML Web services 方法的必要条件之外,您需要向 System.EnterpriseServices.dll 添加引用。该命名空间包含公开在 COM+ 服务中找到的分布式事务模型的方法和属性。System.EnterpriseServices.ContextUtil 类允许您使用 SetAbort 或 SetComplete 方法选择事务。有关更多信息,请参见参与使用 ASP.NET 创建的 XML Web services 中的事务和自动事务和 XML Web services。
使用 XML Web services 方法创建新事务
将引用添加到 System.EnterpriseServices.dll。有关更多信息,请参见添加和移除引用。
将 System.EnterpriseServices 命名空间添加到 XML Web services,如下所示:
' Visual Basic
Imports System.EnterpriseServices
// C#
using System.EnterpriseServices;使用 WebMethod 属性的 TransactionOption 属性,如下所示:
' Visual Basic
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
// C#
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();
}
}请参见
创建托管代码中的 XML Web services | WebMethodAttribute 类