WebMethod有6个属性:
.Description
.EnableSession
.MessageName
.TransactionOption
.CacheDuration
.BufferResponse

1) Description:
是对webservice方法描述的信息。就像webservice方法的功能注释,可以让调用者看见的注释。

C#:

[WebMethod(Description="Author:ZFive5 Function:Hello World") ]
public string HelloWorld()
{
  return "Hello World";
}

WSDL:

- <portType name="Service1Soap">
- <operation name="HelloWorld">
  <documentation>Author:ZFive5 Function:Hello World</documentation> 
  <input message="s0:HelloWorldSoapIn" /> 
  <output message="s0:HelloWorldSoapOut" /> 
  </operation>
  </portType>
- <portType name="Service1HttpGet">
- <operation name="HelloWorld">
  <documentation>Author:ZFive5 Function:Hello World</documentation> 
  <input message="s0:HelloWorldHttpGetIn" /> 
  <output message="s0:HelloWorldHttpGetOut" /> 
  </operation>
  </portType>
- <portType name="Service1HttpPost">
- <operation name="HelloWorld">
  <documentation>Author:ZFive5 Function:Hello World</documentation> 
  <input message="s0:HelloWorldHttpPostIn" /> 
  <output message="s0:HelloWorldHttpPostOut" /> 
  </operation>
  </portType>
  
2)EnableSession:

指示webservice否启动session标志,主要通过cookie完成的,默认false。

C#:

public static int i=0;
[WebMethod(EnableSession=true)]
public int  Count()
{
   i=i+1;
   return i;
}

在ie地址栏输入:
http://localhost/WebService1/Service1.asmx/Count?

点刷新看看

......
<?xml version="1.0" encoding="utf-8" ?> 
  <int xmlns="http://tempuri.org/">19</int
  
<?xml version="1.0" encoding="utf-8" ?> 
  <int xmlns="http://tempuri.org/">20</int>
......

通过它实现webservice数据库访问的事物处理,做过实验,可以哦!

3)MessageName:
主要实现方法重载后的重命名:

在下面的示例中,MessageName 用于消除两个 Add 方法的歧义。
[Visual Basic] 
<%@ WebService Language="VB" class="Calculator" %>

Imports System
Imports System.Web.Services

Public Class Calculator
    Inherits WebService
    
    ' The MessageName property defaults to Add for this XML Web service method.
    <WebMethod()> _
    Overloads Public Function Add(i As Integer, j As Integer) As Integer
        
        Return i + j
    End Function
    
    <WebMethod(MessageName := "Add2")> _
    Overloads Public Function Add(i As Integer, j As Integer, k As Integer) As Integer
        
        Return i + j + k
    End Function    
End Class
[C#] 
<%@ WebService Language="C#" class="Calculator" %>
 
 using System;
 using System.Web.Services;
 
 public class Calculator : WebService {
    // The MessageName property defaults to Add for this XML Web service method.
    [WebMethod]
    public int Add(int i, int j) {
       return i + j;
    }   
    [WebMethod(MessageName="Add2")]
    public int Add(int i, int j, int k) {
       return i + j + k;
    }   
 }

通过Add访问的是第一个方法,而通过Add2访问的是第二个方法!

4)TransactionOption:
指示 XML Web services 方法的事务支持。

这是msdn里的解释:

由于 HTTP 协议的无状态特性,XML Web services 方法只能作为根对象参与事务。
如果 COM 对象与 XML Web services 方法参与相同的事务,并且在组件服务管理工
具中被标记为在事务内运行,XML Web services 方法就可以调用这些 COM 对象。
如果一个 TransactionOption 属性为 Required 或 RequiresNew 的 XML Web services
方法调用 另一个 TransactionOption 属性为 Required 或 RequiresNew 的 XML Web services 方法,
每个 XML Web services 方法将参与它们自己的事务,因为XML Web services 方法只能用作事务中的
根对象。

如果异常是从 Web 服务方法引发的或未被该方法捕获,则自动放弃该事务。如果未发生异常,则自动提
交该事务,除非该方法显式调用 SetAbort。

禁用
 指示 XML Web services 方法不在事务的范围内运行。当处理请求时,将在没有事务
 的情况下执行 XML Web services 方法。 
[WebMethod(TransactionOption= TransactionOption.Disabled)]
 
NotSupported 
指示 XML Web services 方法不在事务的范围内运行。当处理请求时,将在没有事务的
情况下执行 XML Web services 方法。 
[WebMethod(TransactionOption= TransactionOption.NotSupported)]
 
Supported (msdn里写错了,这里改正)

如果有事务,指示 XML Web services 方法在事务范围内运行。如果没有事务,将在没有事务的情况
下创建 XML Web services。 
[WebMethod(TransactionOption= TransactionOption.Supported)]
 
必选 
指示 XML Web services 方法需要事务。由于 Web 服务方法只能作为根对象参与事务,因
此将为 Web 服务方法创建一个新事务。 
[WebMethod(TransactionOption= TransactionOption.Required)]
 
RequiresNew 
指示 XML Web services 方法需要新事务。当处理请求时,将在新事务内创建 XML Web services。 
[WebMethod(TransactionOption= TransactionOption.RequiresNew)]
 
这里我没有实践过,所以只能抄袭msdn,这里请包涵一下了

C#
<%@ WebService Language="C#" class="Bank"%>
<%@ assembly name="System.EnterpriseServices" %>
 
 using System;
 using System.Web.Services;
 using System.EnterpriseServices;
 
 public class Bank : WebService {
 
    [ WebMethod(TransactionOption=TransactionOption.RequiresNew) ]
    public void Transfer(long Amount, long AcctNumberTo, long AcctNumberFrom)  {
      MyCOMObject objBank = new MyCOMObject();
         
      if (objBank.GetBalance(AcctNumberFrom) < Amount )
         // Explicitly abort the transaction.
         ContextUtil.SetAbort();
      else {
         // Credit and Debit methods explictly vote within
         // the code for their methods whether to commit or
         // abort the transaction.
         objBank.Credit(Amount, AcctNumberTo);
         objBank.Debit(Amount, AcctNumberFrom);
      }
    }
 }

5)CacheDuration:
Web支持输出高速缓存,这样webservice就不需要执行多遍,可以提高访问效率,
而CacheDuration就是指定缓存时间的属性。我一般定义为12个小时,对于一些不是需要经常取数据的情况。

C#:
public static int i=0;
[WebMethod(EnableSession=true,CacheDuration=30)]
public int  Count()
{
   i=i+1;
   return i;
}

在ie的地址栏里输入:

http://localhost/WebService1/Service1.asmx/Count?

刷新它,一样吧!要使输出不一样,等30秒。。。
因为代码30秒后才被再次执行,之前返回的结果都是在服务器高速缓存里的内容。

6)BufferResponse

配置WebService方法是否等到响应被完全缓冲完,才发送信息给请求端。普通应用要等完
全被缓冲完才被发送的!看看下面的程序:
通常情况下,只有当已知 XML Web services 方法将大量数据返回到客户端时,才需要将 BufferResponse 设置为 false。对于少量数据,将 BufferResponse 设置为 true 可提高 XML Web services 的性能。

当 BufferResponse 为 false 时,将对 XML Web services 方法禁用 SOAP 扩展名。

C#:

[WebMethod(BufferResponse=false)]
public void HelloWorld1()
{
   int i=0;
   string s="";
   while(i<100)
  {
   s=s+"i<br>";
   this.Context.Response.Write(s);
   i++;
   }
   return;
 }

[WebMethod(BufferResponse=true)]
public void HelloWorld2()
{
   int i=0;
   string s="";
   while(i<100)
  {
   s=s+"i<br>";
   this.Context.Response.Write(s);
   i++;
   }
   return;
 }
 
从两个方法在ie里执行的结果就可以看出他们的不同,第一种,是推技术哦!
有什么数据马上返回,而后一种是把信息一起返回给请求端的。

我的例子本身破坏了webservice返回结构,所以又拿出msdn里的例子来,不要
怪哦!

[C#] 
<%@WebService class="Streaming" language="C#"%>

using System;
using System.IO;
using System.Collections;
using System.Xml.Serialization;
using System.Web.Services;
using System.Web.Services.Protocols;

public class Streaming {

  [WebMethod(BufferResponse=false)]
  public TextFile GetTextFile(string filename) {
    return new TextFile(filename);
  }

  [WebMethod]
  public void CreateTextFile(TextFile contents) {
    contents.Close();
  }

}

public class TextFile {
  public string filename;
  private TextFileReaderWriter readerWriter;

  public TextFile() {
  }

  public TextFile(string filename) {
    this.filename = filename;
  }

  [XmlArrayItem("line")]
  public TextFileReaderWriter contents {
    get {
      readerWriter = new TextFileReaderWriter(filename);
      return readerWriter;
    }
  }

  public void Close() {
    if (readerWriter != null) readerWriter.Close();
  }
}

public class TextFileReaderWriter : IEnumerable {

  public string Filename;
  private StreamWriter writer;

  public TextFileReaderWriter() {
  }

  public TextFileReaderWriter(string filename) {
    Filename = filename;
  }

  public TextFileEnumerator GetEnumerator() {
    StreamReader reader = new StreamReader(Filename);
    return new TextFileEnumerator(reader);
  }

  IEnumerator IEnumerable.GetEnumerator() {
    return GetEnumerator();
  }

  public void Add(string line) {
    if (writer == null)
      writer = new StreamWriter(Filename);
    writer.WriteLine(line);
  }

  public void Close() {
    if (writer != null) writer.Close();
  }

}

public class TextFileEnumerator : IEnumerator {
  private string currentLine;
  private StreamReader reader;

  public TextFileEnumerator(StreamReader reader) {
    this.reader = reader;
  }

  public bool MoveNext() {
    currentLine = reader.ReadLine();
    if (currentLine == null) {
      reader.Close();
      return false;
    }
    else
      return true;
  }

  public void Reset() {
    reader.BaseStream.Position = 0;
  }

  public string Current {
    get {
      return currentLine;
    }
  }

  object IEnumerator.Current {
    get {
      return Current;
    }
  }
}

WebMethod属性详解的更多相关文章

  1. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  2. OutputCache属性详解(一)一Duration、VaryByParam

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  3. OutputCache属性详解(二)一 Location

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  4. OutputCache属性详解(三)— VaryByHeader,VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  5. OutputCache属性详解(四)— SqlDependency

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  6. WPF依赖属性详解

    WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...

  7. HTML video 视频标签全属性详解

    HTML 5 video 视频标签全属性详解   现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.C ...

  8. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  9. dede的pagelist标签的listsize数字属性详解(借鉴)

    dede的pagelist标签的listsize数字属性详解.见远seo经常用织梦搭建各种网站,有次发现列表页面的分页显示超过div的界限,也就是溢出了或者说是撑破了.后来经过研究发现是pagelis ...

随机推荐

  1. 【转载】Linux下makefile详解--跟我一起写 Makefile

    概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...

  2. BZOJ4068 : [Ctsc2015]app

    对于一个所选任务集合,如果对于任意时刻$i$,$i$前面所选任务数都不超过i的话,那么这些任务可以全选. 维护一棵线段树$T$,第$i$个位置一开始为$i$,每使用一个任务,$[t,T]$都要减$1$ ...

  3. BZOJ3619 : [Zjoi2014]璀灿光华

    终于把省选时的遗憾补上了… 对于构造立方体: 首先BFS构出底层,然后再逐层构造立方体 对于计算: $O(n^6)$爆搜即可. #include<cstdio> #include<c ...

  4. COJ980 WZJ的数据结构(负二十)

    试题描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只 ...

  5. Git补丁

    引子: 上班有问题没有解决,在家里搞定了,于是把改动打成一个补丁,明天应用到公司的工作电脑上.(以下内容转自别处) 1.创建补丁,比如把最新的两次提交纪录转化为补丁文件,可以用如下命令: git fo ...

  6. 如何设置NBU的Backup, Archive and Restore

    第一行表示Master Server 第二行猜测表示当前主机 第三行不清楚   总结: 1. 这里压根就没有Media Server的事, Media Server对整个备份系统来说是透明的. 2. ...

  7. 【液晶模块系列基础视频】4.2.X-GUI图形界面库-画矩形函数简介

    [液晶模块系列基础视频]4.2.X-GUI图形界面库-画矩形函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地址: ...

  8. 【iCore2双核心板视频教程三】iM_LAN 100M 以太网模块TCP压力测试(更新视频教程)

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  9. 分布式架构高可用架构篇_03-redis3集群的安装高可用测试

    参考文档 Redis 官方集群指南:http://redis.io/topics/cluster-tutorial Redis 官方集群规范:http://redis.io/topics/cluste ...

  10. Java集合之Collection接口

    java的集合分为三大接口,分别是Collection,Map,Iterator,集合接口和类在java.util包中,此次主要介绍三大接口之一的Collection接口. 一些Collection允 ...