国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.Net下WebMethod屬性

2024-07-10 12:59:17
字體:
來源:轉載
供稿:網友
菜鳥學堂:
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:

主要實現方法重載后的重命名:

c#:

public static int i=0;
[webmethod(enablesession=true)]
public int count()
{
i=i+1;
return i;
}

[webmethod(enablesession=true,messagename="count1")]
public int count(int da)
{
i=i+da;
return i;
}


通過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就是指定緩存時間的屬性。

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方法是否等到響應被完全緩沖完,才發送信息給請求端。普通應用要等完
全被緩沖完才被發送的!看看下面的程序:

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;
}
}
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延安市| 丰宁| 荆门市| 满城县| 紫云| 武夷山市| 扬州市| 青河县| 乌兰察布市| 徐水县| 中超| 达日县| 乐至县| 郁南县| 彭泽县| 乌兰浩特市| 松江区| 遂宁市| 囊谦县| 东乡族自治县| 尉氏县| 沅陵县| 道真| 安西县| 樟树市| 沅江市| 汕尾市| 垫江县| 天津市| 南汇区| 花莲县| 刚察县| 台山市| 铜鼓县| 乌什县| 蚌埠市| 云浮市| 五莲县| 巴东县| 剑河县| 剑阁县|