程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .Net下WebMethod屬性

.Net下WebMethod屬性

編輯:C#入門知識

Author:zfive5(zhaozidong)
Email :[email protected]



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


[XmlArra

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved