使用 WebMethod 属性

将 WebMethod 属性 (Attribute) 附加到 Public 方法表示希望将该方法公开为 XML Web services 的一部分。您还可以使用该属性 (Attribute) 的属性 (Property) 进一步配置 XML Web services 方法的行为。有关更多信息,请参见托管代码中的 XML Web services 的代码模型。

WebMethod 属性 (Attribute) 提供以下属性 (Property):

 

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),如下所示:

     

    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),如下所示:

     

    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),如下所示:

     

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),如下所示:

     

    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),如下所示:

     

     

    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.1Host: localhostContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "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 OKContent-Type: text/xml; charset=utf-8Content-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 方法创建新事务

 

  1.  

    添加一个对 System.EnterpriseServices.dll 的引用。有关更多信息,请参见添加和移除引用。

  2.  

    将 System.EnterpriseServices 命名空间添加到 XML Web services,如下所示:

    Visual Basic

    复制代码

    Imports System.EnterpriseServices

     

    C#

    复制代码

    using System.EnterpriseServices;

     

  3.  

    使用 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 FunctionEnd 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(); }}

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值