.net 下webservice 的WebMethod的属性
| WebMethod有6个属性:.Description.EnableSession.MessageName.TransactionOption.CacheDuration.BufferResponse1) Description:是对webservice方法描述的信息。就像webservice方法的功能注释,可以让调用者看见的注释。C#:[WebMethod(Description="Author:ZFive5 Function:Hello World") ]publicstringHelloWorld(){  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#:publicstaticinti=0;[WebMethod(EnableSession=true)]publicintCount(){   i=i+1;   returni;}在ie地址栏输入:http://localhost/WebService1/Service1.asmx/Count?点刷新看看......<?XML version="1.0"encoding="utf-8"?>   <intxmlns="19http://tempuri.org/">19</int>   <?xml version="1.0"encoding="utf-8"?>   <intxmlns="20http://tempuri.org/">20</int>............通过它实现webservice数据库访问的事物处理,做过实验,可以哦!3)MessageName:主要实现方法重载后的重命名:C#:publicstaticinti=0;[WebMethod(EnableSession=true)]publicintCount(){     i=i+1;   returni;}[WebMethod(EnableSession=true,MessageName="Count1")]publicintCount(intda){   i=i+da;   returni;}通过count访问的是第一个方法,而通过count1访问的是第二个方法! 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"%>  usingSystem; usingSystem.Web.Services; usingSystem.EnterpriseServices;  publicclassBank : WebService {     [ WebMethod(TransactionOption=TransactionOption.RequiresNew) ]    publicvoidTransfer(longAmount, longAcctNumberTo, longAcctNumberFrom)  {      MyCOMObject objBank = newMyCOMObject();               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就是指定缓存时间的属性。C#:publicstaticinti=0;[WebMethod(EnableSession=true,CacheDuration=30)]publicintCount(){   i=i+1;   returni;}在ie的地址栏里输入:http://localhost/WebService1/Service1.asmx/Count?刷新它,一样吧!要使输出不一样,等30秒。。。因为代码30秒后才被再次执行,之前返回的结果都是在服务器高速缓存里的内容。6)BufferResponse配置WebService方法是否等到响应被完全缓冲完,才发送信息给请求端。普通应用要等完全被缓冲完才被发送的!看看下面的程序:C#:[WebMethod(BufferResponse=false)]publicvoidHelloWorld1(){   inti=0;   strings="";   while(i<100)  {   s=s+"i<br>";   this.Context.Response.Write(s);   i++;   }   return; }  [WebMethod(BufferResponse=true)]publicvoidHelloWorld2(){   inti=0;   strings="";   while(i<100)  {   s=s+"i<br>";   this.Context.Response.Write(s);   i++;   }   return; } 从两个方法在ie里执行的结果就可以看出他们的不同,第一种,是推技术哦!有什么数据马上返回,而后一种是把信息一起返回给请求端的。我的例子本身破坏了webservice返回结构,所以又拿出msdn里的例子来,不要怪哦![C#] <%@WebService class="Streaming"language="C#"%>usingSystem;usingSystem.IO;usingSystem.Collections;usingSystem.Xml.Serialization;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;publicclassStreaming {  [WebMethod(BufferResponse=false)]  publicTextFile GetTextFile(stringfilename) {    returnnewTextFile(filename);  }  [WebMethod]  publicvoidCreateTextFile(TextFile contents) {    contents.Close();  }}publicclassTextFile {  publicstringfilename;  privateTextFileReaderWriter readerWriter;  publicTextFile() {  }  publicTextFile(stringfilename) {    this.filename = filename;  }  [XmlArrayItem("line")]  publicTextFileReaderWriter contents {    get{      readerWriter = newTextFileReaderWriter(filename);      returnreaderWriter;    }  }  publicvoidClose() {    if(readerWriter != null) readerWriter.Close();  }}publicclassTextFileReaderWriter : IEnumerable {  publicstringFilename;  privateStreamWriter writer;  publicTextFileReaderWriter() {  }  publicTextFileReaderWriter(stringfilename) {    Filename = filename;  }  publicTextFileEnumerator GetEnumerator() {    StreamReader reader = newStreamReader(Filename);    returnnewTextFileEnumerator(reader);  }  IEnumerator IEnumerable.GetEnumerator() {    returnGetEnumerator();  }  publicvoidAdd(stringline) {    if(writer == null)      writer = newStreamWriter(Filename);    writer.WriteLine(line);  }  publicvoidClose() {    if(writer != null) writer.Close();  }}publicclassTextFileEnumerator : IEnumerator {privatestringcurrentLine;  privateStreamReader reader;  publicTextFileEnumerator(StreamReader reader) {    this.reader = reader;  }  publicboolMoveNext() {    currentLine = reader.ReadLine();    if(currentLine == null) {      reader.Close();      returnfalse;    }    else      returntrue;  }  publicvoidReset() {    reader.BaseStream.Position = 0;  }  publicstringCurrent {    get{      returncurrentLine;    }  }  objectIEnumerator.Current {    get{      returnCurrent;    }  }} | 
编辑器加载中...
.net 下webservice 的WebMethod的属性的更多相关文章
- jQuery Ajax方法调用 Asp.Net WebService、WebMethod 的详细实例代码
		将以下html存为ws.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <scri ... 
- 今天研究了下webservice 终于OK了
		今天研究了下webservice 终于OK了,所以把它写到自己的博客来,因为网上说的都很复杂 而在这里,我会很简单的说明,一看就懂 首先在进行webservice 一定要下载包 ... 
- python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名
		python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名,如果在类中定义的就是类的私有成员. >>> dir(__builtin ... 
- 【转载】CentOS下查看电脑硬件设备属性命令
		CentOS下查看电脑硬件设备属性命令2018年09月13日 17:48:31 乔烨 阅读数 510如何在linux下查看电脑硬件设备属性 # uname -a # 查看内核/操作系统/CPU信息 # ... 
- 传统模式下WebService与WebAPI的相同与不同
		1.WebService是利用HTTP管道实现了RPC的一种规范形式,放弃了对HTTP原生特征与语义的完备支持:而WebAPI是要保留HTTP原生特征与语义的同时实现RPC,但WebAPI的实现风格可 ... 
- 让IE下支持Html5的placeholder属性
		HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等. Placeholder 是HTML5新增的另一个属性,当input或者textarea设 ... 
- C语言下WebService的使用方式
		用gSoap工具: 1.在dos环境中到gSoap工具对应的目录gsoap_2.8.18\gsoap-2.8\gsoap\bin\win32路径下,执行wsdl2h -c -o *.h ht ... 
- IE下设置unselectable与onselectstart属性的bug,Firefox与Chrome下的解决方案
		在IE下给DIV设置unselectable与onselectstart属性,可以让div的内容不能选中,这个功能在很多情况下,非常有用,但是他的bug太明显, 直接使用一个DIV是可以的,比如: & ... 
- IE下script标签的readyState属性
		在做加载器时遇到一个常见问题,如何判定一个脚本已经执行完毕. "uninitialized" – 原始状态 "loading" – 下载数据中 "lo ... 
随机推荐
- gdb生成的core文件位置
			gdb可以生成core文件,记录堆栈信息,core文件名字是下面这种格式 :core.9488,其中9488是PID 文件位置是当前目录 
- 单点登录CAS-Demo
			版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 1安全证书配置 2部署服务端CAS-Server 3部署CAS-Client 4测试SSO 1,安全证书配置 CAS默认 ... 
- Fragment 生命周期怎么来的?
			前言 Fragment对于 Android 开发人员来说一点都不陌生,由于差点儿不论什么一款 app 都大量使用 Fragment,所以 Fragment 的生命周期相信对于大家来说应该都非常清晰.但 ... 
- python 工具ScreenShoot
			环境:windows python3 # -*- coding: UTF-8 -*- import time import os, win32gui, win32ui, win32con, win32 ... 
- BUPT复试专题—统计节点个数(2013)
			题目描述 给出一棵有向树,一共有n个节点,如果一个节点的度(入度+出度)不小于它所有儿子以及它父亲的度(如果存在父亲或儿子),那么我们称这个节点为p节点,现在你的任务是统计p节点的个数. 如样例,第一 ... 
- 上篇:es5、es6、es7中的异步写法
			本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/es-async 本博客同步在http://www.cnbl ... 
- Dynamics CRM 2015/2016 Web API:新的数据查询方式
			今天我们来看看Web API的数据查询功能,尽管之前介绍CRUD的文章里面提到过怎么去Read数据,可是并没有详细的去深究那些细节,今天我们就来详细看看吧.事实上呢,Web API的数据查询接口也是基 ... 
- 【转载】VS工具使用——代码图
			代码图: 心想,反正也调不出来,就试试这个东西吧,一打开,就认识到自己发现了一个新大陆:这个代码图可以让我们对一个工程文件有大体的了解,即函数的调用关系等.它是一个VS2013自带工具生成函数 ... 
- wamp配置虚拟域名
			1.打开apache下httpd.conf 我的目录是在F:\wamp\bin\apache\apache2.2.22\conf\httpd.conf 2.去掉这两行前面的#注释 LoadModule ... 
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数
			// 二维数组中的查找,杨氏矩阵在一个二维数组中.每行都依照从左到右的递增的顺序排序. // 每列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个数组和一个数.推断数组中是否包括这个数 #i ... 
