WebService 主要包含 WebService 、SoapDocumentService、WebServiceBinding三个属性。若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,需取消对下行的注释。
  // [System.Web.Script.Services.ScriptService]

  3、WebService 所暴露给调用者的方法带有 [WebMethod] 属性,有6个属性:Description、EnableSession、MessageName、TransactionOption、CacheDuration、BufferResponse。

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

  1. [WebMethod(Description="方法:Hello World") ]
  2. public string HelloWorld()
  3. {
  4.   return "Hello World";
  5. }

WSDL:

  1. - <portType name="Service1Soap">
  2. - <operation name="HelloWorld">
  3. <documentation>方法:Hello World</documentation>
  4. <input message="s0:HelloWorldSoapIn" />
  5. <output message="s0:HelloWorldSoapOut" />
  6. </operation>
  7. </portType>
  8. - <portType name="Service1HttpGet">
  9. - <operation name="HelloWorld">
  10. <documentation>Author:ZFive5 Function:Hello World</documentation>
  11. <input message="s0:HelloWorldHttpGetIn" />
  12. <output message="s0:HelloWorldHttpGetOut" />
  13. </operation>
  14. </portType>
  15. - <portType name="Service1HttpPost">
  16. - <operation name="HelloWorld">
  17. <documentation>Author:ZFive5 Function:Hello World</documentation>
  18. <input message="s0:HelloWorldHttpPostIn" />
  19. <output message="s0:HelloWorldHttpPostOut" />
  20. </operation>
  21. </portType>

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

  1. public static int i=;
  2. [WebMethod(EnableSession=true)]
  3. public int Count()
  4. {
  5. i=i+;
  6. return i;
  7. }

  在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 方法的歧义。

  1. using System;
  2. using System.Web.Services;
  3.  
  4. public class Calculator : WebService {
  5. // The MessageName property defaults to Add for this XML Web service method.
  6. [WebMethod]
  7. public int Add(int i, int j) {
  8. return i + j;
  9. }
  10. [WebMethod(MessageName="Add2")]
  11. public int Add(int i, int j, int k) {
  12. return i + j + k;
  13. }
  14. }

  通过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个小时,对于一些不是需要经常取数据的情况。

  1. public static int i=;
  2. [WebMethod(EnableSession=true,CacheDuration=)]
  3. public int Count()
  4. {
  5. i=i+;
  6. return i;
  7. }

  在ie的地址栏里输入:

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

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

  [6] BufferResponse

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

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

  1. [WebMethod(BufferResponse=false)]
  2. public void HelloWorld1()
  3. {
  4. int i=;
  5. string s="";
  6. while(i<)
  7. {
  8.    s=s+"i<br>";
  9.    this.Context.Response.Write(s);
  10.    i++;
  11. }
  12. return;
  13. }
  14.  
  15. [WebMethod(BufferResponse=true)]
  16. public void HelloWorld2()
  17. {
  18. int i=;
  19. string s="";
  20. while(i<)
  21. {
  22.    s=s+"i<br>";
  23.    this.Context.Response.Write(s);
  24.    i++;
  25. }
  26. return;
  27. }

从两个方法在ie里执行的结果就可以看出他们的不同,第一种,是推技术哦!

有什么数据马上返回,而后一种是把信息一起返回给请求端的。

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

  1. <%@WebService class="Streaming" language="C#"%>
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Xml.Serialization;
  7. using System.Web.Services;
  8. using System.Web.Services.Protocols;
  9.  
  10. public class Streaming {
  11.  
  12.   [WebMethod(BufferResponse=false)]
  13.   public TextFile GetTextFile(string filename) {
  14.     return new TextFile(filename);
  15.   }
  16.  
  17.   [WebMethod]
  18.   public void CreateTextFile(TextFile contents) {
  19.     contents.Close();
  20.   }
  21.  
  22. }
  23.  
  24. public class TextFile {
  25.   public string filename;
  26.   private TextFileReaderWriter readerWriter;
  27.  
  28.   public TextFile() {
  29.   }
  30.  
  31.   public TextFile(string filename) {
  32.     this.filename = filename;
  33.   }
  34.  
  35.   [XmlArrayItem("line")]
  36.   public TextFileReaderWriter contents {
  37.     get {
  38.       readerWriter = new TextFileReaderWriter(filename);
  39.       return readerWriter;
  40.     }
  41.   }
  42.  
  43.   public void Close() {
  44.     if (readerWriter != null) readerWriter.Close();
  45.   }
  46. }
  47.  
  48. public class TextFileReaderWriter : IEnumerable {
  49.  
  50.   public string Filename;
  51.   private StreamWriter writer;
  52.  
  53.   public TextFileReaderWriter() {
  54.   }
  55.  
  56.   public TextFileReaderWriter(string filename) {
  57.     Filename = filename;
  58.   }
  59.  
  60.   public TextFileEnumerator GetEnumerator() {
  61.     StreamReader reader = new StreamReader(Filename);
  62.     return new TextFileEnumerator(reader);
  63.   }
  64.  
  65.   IEnumerator IEnumerable.GetEnumerator() {
  66.     return GetEnumerator();
  67.   }
  68.  
  69.   public void Add(string line) {
  70.     if (writer == null)
  71.       writer = new StreamWriter(Filename);
  72.     writer.WriteLine(line);
  73.   }
  74.  
  75.   public void Close() {
  76.     if (writer != null) writer.Close();
  77.   }
  78.  
  79. }
  80.  
  81. public class TextFileEnumerator : IEnumerator {
  82.   private string currentLine;
  83.   private StreamReader reader;
  84.  
  85.   public TextFileEnumerator(StreamReader reader) {
  86.     this.reader = reader;
  87.   }
  88.  
  89.   public bool MoveNext() {
  90.     currentLine = reader.ReadLine();
  91.     if (currentLine == null) {
  92.       reader.Close();
  93.       return false;
  94.     }
  95.     else
  96.       return true;
  97.   }
  98.  
  99.   public void Reset() {
  100.     reader.BaseStream.Position = ;
  101.   }
  102.  
  103.   public string Current {
  104.     get {
  105.       return currentLine;
  106.     }
  107.   }
  108.  
  109.   object IEnumerator.Current {
  110.     get {
  111.       return Current;
  112.     }
  113.   }
  114. }

MSDN实例

WebService 之 属性详解的更多相关文章

  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. Unity 2D游戏开发教程之摄像头追踪功能

    Unity 2D游戏开发教程之摄像头追踪功能 上一章,我们创建了一个简单的2D游戏.此游戏中的精灵有3个状态:idle.left和right.这看起来确实很酷!但是仅有的3个状态却限制了精灵的能力,以 ...

  2. shell中的cat和文件分界符(<<EOF) (转)

    原文地址: http://blog.csdn.net/mosesmo1989/article/details/51123257 在shell中,文件分界符(通常写成EOF,你也可以写成FOE或者其他任 ...

  3. pm2常用的命令

    exit //退出 ssh www@25.17.1.54 // 远程登录主机sudo su // 获得 root 权限 并且进入目录 /home/wwwpm2 list // 查看当前的列表 id 和 ...

  4. 【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 164  Solved: 75 Description SD有一名神犇叫做Oxe ...

  5. [BZOJ2431][HAOI2009]逆序对数列(DP)

    从小到大加数,根据加入的位置转移,裸的背包DP. #include<cstdio> #include<cstring> #include<algorithm> #d ...

  6. Navicat连接Docker中的mysql报错:client does not support authentication

    1.进入mysql容器中 docker exec -it mysqltest(mysql容器名称) bash 2.进入mysql数据库 mysql -uroot -p 3.输入mysql密码 4.远程 ...

  7. JDK源码(1.7) -- java.util.Deque<E>

    java.util.Deque<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...

  8. python开发_tarfile_文档归档压缩|解压缩

    ''' python中的tarfile模块实现文档的归档压缩和解压缩 功能: 把工作空间下面的所有文件,打包生成一个tar文件 同时提供一个方法把该tar文件中的一些文件解压缩到 指定的目录中 ''' ...

  9. asp.net url传值,弹窗

    一,<a>标签链接式传值 1, <a href="News_list.aspx?ClassID=<%#((DataRowView)Container.DataItem ...

  10. HDU 5305 Friends dfs

    Friends 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5305 Description There are n people and m pa ...