Web Method's Attributes

Enabling a Session State for a Web Method

You can configure a Web method to maintain the state of objects across sessions. The EnableSession property of the WebMethod attribute allows you to enable the  session state for a Web method. If you set the EnableSession property to True, the XML Web service accesses the session state collection directly from the Http Context.Current.Session session or the WebService.Session property if it is inherited from the WebService base class. The default value of the EnableSession property is False. If you don't set EnableSession property to True and accesse the session state collection, it will crash.

public class Service1 : System.Web.Services.WebService
{
    [WebMethod(EnableSession=true)]
    public double ConvertPounds (double kgs)
    {
        Session["Conversions"] = (int) Session["Conversions"] + 1;
        return (kgs * .45);
    }

    [WebMethod(EnableSession=true)]
    public int GetNumberOfConversions()
    {
        return (int) Session["Conversions"];
    }
}

Identifying Overloaded Web Methods Using an Alias

You can use the MessageName property of the WebMethod attribute to uniquely identify overloaded Web methods. The MessageName property enables you to use an alias to uniquely identify Web methods in an XML Web service. When you specify a value for the MessageName property, the SOAP messages use this value as the method name instead of the actual method name.

public class Service1 : System.Web.Services.WebService
{
    [WebMethod(MessageName="AddIntegers")]
    public int AddNumbers(int num1, int num2)
    {
        return (num1+num2);
    }
    [WebMethod(MessageName="AddLongs")]
    public long AddNumbers(long num1, long num2)
    {
        return (num1+num2);
    }

}
//如果要对WebMethod做重载,必须先将类的WebServiceBinding的ConformsTo 设为 WsiProfiles.None.否则,无法声明同样名字  //的Web service.MessageName绝对不可以相同。

Specifying a Description for a Web Method

The Description property enables you to specify a description for a Web method. The description that you specify using the Description property appears on the  Service help page. The Description property takes a string as an input value.

public class Service1 : System.Web.Services.WebService
{
    [WebMethod(Description="Takes two integer values and returns " +
    "their sum.")]
    public int AddNumbers(int num1, int num2)
    {
        return (num1+num2);
    }
}

Caching the Results for a Web Method

The CacheDuration property of the WebMethod attribute enables you to cache the results for a Web method. ASP.NET caches the results for each unique parameter set for the duration that you specify as the value of this property. The default value of the CacheDuration property is zero.

public class Service1 : System.Web.Services.WebService
{
    [WebMethod(CacheDuration=60)]
    public int AddNumbers(int num1, int num2)
    {
        return (num1+num2);
    }
}

Buffering Responses for a Web Method

You buffer responses for a Web method to improve the performance of an application by reducing communication between the worker process and the IIS process. To buffer responses for a Web method, you use the BufferResponse property of the WebMethod attribute. When you set the BufferResponse property to True, ASP.NET buffers all the responses before communicating them to the client. If you set the BufferResponse property to False, ASP.NET buffers the responses in chunks of 16 KB each. The default value of the BufferResponse property is True.

public class Service1 : System.Web.Services.WebService
{
    [WebMethod (BufferResponse=false)]
    public int AddNumbers(int num1, int num2)
    {
        return (num1+num2);
    }
}

如果将 BufferResponse 设置为 true,就会将 XML Web services 方法的响应序列化到内存缓冲区中,直到该响应被完全序列化或缓冲区已满为止。一旦完成对响应的缓冲处理,该响应就通过网络返回到 XML Web services 客户端。当 BufferResponse 为 false 时,对 XML Web services 方法的响应在被序列化的同时被发回客户端。通常情况下,只有当已知 XML Web services 方法将大量数据返回到客户端时,才需要将 BufferResponse 设置为 false。对于少量数据,将 BufferResponse 设置为 true 可提高 XML Web services 的性能。当 BufferResponse 为 false 时,将对 XML Web services 方法禁用 SOAP 扩展名。

TransactionOption

WebMethod 特性的 TransactionOption 属性使 XML Web services 方法可以作为事务的根对象参与。虽然可以将 TransactionOption 属性设置为 TransactionOption 枚举的任意值,但 XML Web services 方法仅有两个可能的行为:它不参与事务(Disabled、NotSupported、Supported)或它创建一个新事务(Required、RequiresNew)。除非另外指定,默认值为 TransactionOption.Disabled。
除了任何 XML Web services 方法的必要条件之外,您需要向 System.EnterpriseServices.dll 添加引用。该命名空间包含公开在 COM+ 服务中找到的分布式事务模型的方法和属性。System.EnterpriseServices.ContextUtil 类允许您使用 SetAbort 或 SetComplete 方法选择事务。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值