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方法的功能注釋,可以讓調用者看見的注釋。
[WebMethod(Description="方法:Hello World") ] public string HelloWorld() { return "Hello World"; }
WSDL:
- <portType name="Service1Soap"> - <operation name="HelloWorld"> <documentation>方法: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。
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:主要實現方法重載后的重命名
在下面的示例中,MessageName 用於消除兩個 Add 方法的歧義。
using System; using System.Web.Services; public class Calculator : WebService { // The MessageName property defaults to Add for this XML Web service method. [WebMethod] public int Add(int i, int j) { return i + j; } [WebMethod(MessageName="Add2")] public int Add(int i, int j, int k) { return i + j + k; } }
通過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個小時,對於一些不是需要經常取數據的情況。
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方法是否等到響應被完全緩沖完,才發送信息給請求端。普通應用要等完全被緩沖完才被發送的!
看看下面的程序:
通常情況下,只有當已知 XML Web services 方法將大量數據返回到客戶端時,才需要將 BufferResponse 設置為 false。對於少量數據,將 BufferResponse 設置為 true 可提高 XML Web services 的性能。
當 BufferResponse 為 false 時,將對 XML Web services 方法禁用 SOAP 擴展名。
[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里的例子來,不要
怪哦!

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