.Net下WebMethod屬性

發表于:2007-05-25來源:作者:點擊數: 標簽:WebMethodzfiveAuthor.NET屬性
Author:zfive5(zhaozidong) Email :zfive5@yahoo.com.cn WebMethod有6個屬性: .Description .EnableSession .MessageName .TransactionOption .CacheDuration .BufferResponse 1) Description: 是對webservice方法描述的信息。就像webservice方法的功能注釋
Author:zfive5(zhaozidong)
Email :zfive5@yahoo.com.cn



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 Aclearcase/" target="_blank" >cctNumberTo, 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;
}
}
}


原文轉自:http://www.anti-gravitydesign.com

評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97