WebService中webmethod的六个属性
发布时间:2020-12-17 00:12:50 所属栏目:安全 来源:网络整理
导读:webmethod有6个属性: .description .enablesession .messagename .transactionoption .cacheduration .bufferresponse 1) description: 是对webservice方法描述的信息。就像webservice方法的功能注释,可以让调用者看见 的注释。 c#: [webmethod(descriptio
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"> <input message="s0:helloworldhttpgetin" />? <output message="s0:helloworldhttpgetout" />? - <porttype name="service1httppost"> <input message="s0:helloworldhttppostin" />? <output message="s0:helloworldhttppostout" />? 2)enablesession: 指示webservice否启动session标志,主要通过cookie完成的,默认false。 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>? <int xmlns="http://tempuri.org/">20</int> 通过它实现webservice数据库访问的事物处理,做过实验,可以哦! 3)messagename: 主要实现方法重载后的重命名: {? [webmethod(enablesession=true,messagename="count1")] public int count(int da) i=i+da; 通过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" %> 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就是指定缓存时间的属性。 在ie的地址栏里输入: 刷新它,一样吧!要使输出不一样,等30秒。。。 因为代码30秒后才被再次执行,之前返回的结果都是在服务器高速缓存里的内容。 6)bufferresponse 配置webservice方法是否等到响应被完全缓冲完,才发送信息给请求端。普通应用要等完 全被缓冲完才被发送的!看看下面的程序: [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() 从两个方法在ie里执行的结果就可以看出他们的不同,第一种,是推技术哦! 有什么数据马上返回,而后一种是把信息一起返回给请求端的。 我的例子本身破坏了webservice返回结构,所以又拿出msdn里的例子来,不要 怪哦! [c#]? <%@webservice class="streaming" language="c#"%> using system.io; using system.collections; using system.xml.serialization; using system.web.services.protocols; public class streaming { 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 { 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); 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 { return currentline; object ienumerator.current { return current; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |