在ASP.NET WebService 中如何使用 WebMethod 属性
2009-02-07 14:24
459 查看
如何:使用 WebMethod 属性
将 WebMethod 属性 (Attribute) 附加到 Public 方法表示希望将该方法公开为 XML Web services 的一部分。您还可以使用该属性 (Attribute) 的属性 (Property) 进一步配置 XML Web services 方法的行为。有关更多信息,请参见托管代码中的 XML Web services 的代码模型。
WebMethod 属性 (Attribute) 提供以下属性 (Property):
BufferResponse
CacheDuration
Description
EnableSession
MessageName
TransactionOption
Visual Basic
复制代码
C#
复制代码
Visual Basic
复制代码
C#
复制代码
复制代码
Visual Basic
复制代码
C#
复制代码
Visual Basic
复制代码
C#
复制代码
添加双精度型 (AddDoubles) 方法的 SOAP 请求消息将类似于以下代码:
复制代码
添加双精度型 (AddDoubles) 方法的 SOAP 响应消息将类似于以下代码:
复制代码
除了任何 XML Web services 方法的必备条件外,您还需要添加一个对 System.EnterpriseServices.dll 的引用。该命名空间包含了公开在 COM+ Services 中找到的分布式事务模型的方法和属性 (Property)。System.EnterpriseServices.ContextUtil 类允许您使用 SetAbort 或 SetComplete 方法选择事务。有关更多信息,请参见参与使用 ASP.NET 创建的 XML Web services 中的事务和自动事务和 XML Web services。
将 System.EnterpriseServices 命名空间添加到 XML Web services,如下所示:
Visual Basic
复制代码
C#
复制代码
使用 WebMethod 属性 (Attribute) 的 TransactionOption 属性 (Property),如下所示:
Visual Basic
复制代码
C#
复制代码
将 WebMethod 属性 (Attribute) 附加到 Public 方法表示希望将该方法公开为 XML Web services 的一部分。您还可以使用该属性 (Attribute) 的属性 (Property) 进一步配置 XML Web services 方法的行为。有关更多信息,请参见托管代码中的 XML Web services 的代码模型。
WebMethod 属性 (Attribute) 提供以下属性 (Property):
BufferResponse
CacheDuration
Description
EnableSession
MessageName
TransactionOption
BufferResponse
WebMethod 属性 (Attribute) 的 BufferResponse 属性 (Property) 启用对 XML Web services 方法响应的缓冲。当设置为 true(默认设置)时,ASP.NET 在将响应向下发送到客户端之前对整个响应进行缓冲。缓冲非常有效,它通过最小化辅助进程和 IIS 进程之间的通信来帮助提高性能。当设置为 false 时,ASP.NET 以 16KB 的块区缓冲响应。通常,只有在不想将响应的全部内容一次缓冲到内存时,才将该属性 (Property) 设置为 false。例如,您在反写一个集合,该集合正在以流的形式从数据库输出其项。除非另外指定,默认值为 true。有关更多信息,请参见 WebMethodAttribute.BufferResponse 属性 (Property)。缓冲 XML Web services 方法的响应
使用 WebMethod 属性 (Attribute) 的 BufferResponse 属性 (Property),如下所示: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 属性 (Attribute) 的 CacheDuration 属性 (Property) 启用对 XML Web services 方法结果的缓存。ASP.NET 将缓存每个唯一参数集的结果。该属性 (Property) 的值指定 ASP.NET 应该对结果进行多少秒的缓存处理。值为零,则禁用对结果进行缓存。除非另外指定,默认值为零。有关更多信息,请参见 WebMethodAttribute.CacheDuration 属性 (Property)。缓存 XML Web services 方法的结果
使用 WebMethod 属性 (Attribute) 的 CacheDuration 属性 (Property),如下所示: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; } }
说明
WebMethod 属性 (Attribute) 的 Description 属性 (Property) 提供 XML Web services 方法的说明,该说明将显示在服务帮助页上。除非另外指定,默认值为空字符串。有关更多信息,请参见 WebMethodAttribute.Description 属性 (Property)。提供 XML Web services 方法的说明
使用 WebMethod 属性 (Attribute) 的 Description 属性 (Property),如下所示:复制代码
' 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 属性 (Attribute) 的 EnableSession 属性 (Property) 启用 XML Web services 方法的会话状态。一旦启用,XML Web services 就可以从 HttpContext.Current.Session 中直接访问会话状态集合,或者,如果它是从 WebService 基类继承的,则可以使用 WebService.Session 属性来访问会话状态集合。除非另外指定,默认值为 false。有关更多信息,请参见 WebMethodAttribute.EnableSession 属性 (Property)。在 XML Web services 方法中启用会话状态
使用 WebMethod 属性 (Attribute) 的 EnableSession 属性 (Property),如下所示: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 属性 (Attribute) 的 MessageName 属性 (Property) 使 XML Web services 能够唯一确定使用别名的重载方法。除非另外指定,默认值是方法名称。当指定 MessageName 时,结果 SOAP 消息将反映该名称,而不是实际的方法名称。有关更多信息,请参见 WebMethodAttribute.MessageName 属性 (Property)。为 XML Web services 方法提供消息名
使用 WebMethod 属性 (Attribute) 的 MessageName 属性 (Property),如下所示: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 属性 (Attribute) 的 TransactionOption 属性 (Property) 使 XML Web services 方法可以作为事务的根对象参与。虽然可以将 TransactionOption 属性 (Property) 设置为 TransactionOption 枚举的任意值,但 XML Web services 方法仅有两个可能的行为:它不参与事务(Disabled、NotSupported、Supported)或它创建一个新事务(Required、RequiresNew)。除非另外指定,默认值为 TransactionOption.Disabled。有关更多信息,请参见 WebMethodAttribute.TransactionOption 属性 (Property)。除了任何 XML Web services 方法的必备条件外,您还需要添加一个对 System.EnterpriseServices.dll 的引用。该命名空间包含了公开在 COM+ Services 中找到的分布式事务模型的方法和属性 (Property)。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 属性 (Attribute) 的 TransactionOption 属性 (Property),如下所示:
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(); } }
相关文章推荐
- 在ASP.NET WebService 中如何使用 WebMethod 属性
- 如何在ASP.NET中制作Web用户自定义控件,并在aspx页面中使用它的方法和属性?
- 是使用 ASP.NET Web 服务还是使用 .NET Remoting:如何选择
- 在asp.net webservice中如何使用session
- Asp.Net连接ACCESS数据库相对路径问题 web.config中如何使用相对路径及在类文件中调用这个相对路径
- 是使用 ASP.NET Web 服务还是使用 .NET Remoting:如何选择
- 如何在ASP.NET 2.0中使用 Web Parts
- ASP.NET MVC 3 使用 Razor 如何在 Web.config 宣告預設的 Namespaces
- 【转】在asp.net webservice中如何使用session
- Asp.Net 2.0连接ACCESS数据库相对路径问题,web.config中如何使用相对路径
- 使用 jQuery 调用 ASP.NET WebMethod
- 新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- MattPowell介绍了如何在服务器端使用异步Web方法,来创建高性能的 Microsoft ASP.NET Web 服务
- Asp.Net连接ACCESS数据库相对路径问题,web.config中如何使用相对路径及在类文件中调用这个相对路径
- [引]ASP.NET 移动网页 与 如何:使用仿真程序和浏览器在部署移动 Web 应用程序之前对其进行测试
- ASP.NETWeb服务器验证控件如何使用
- 在asp.net webservice中如何使用session
- 在asp.net webservice中如何使用session
- 新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)