[ASP.NET]指定Web Service單向作業

  • 3972
  • 0
  • C#
  • 2011-03-15

[ASP.NET]指定Web Service單向作業

最近在專案中開發了一個資料轉換作業的Web Service,

由於該作業執行時間很久,所以同事建議我指定單向作業,不用讓Client等待,

這篇主要想驗證單向作業是否真的一去不回頭(....XD)。

 

.asmx.cs

		
		[SoapDocumentMethod( OneWay = true )]          
		[WebMethod]  
		public void GetAllData()
		{
		DateTime Starttime = DateTime.Now;
		DataTable dt = new DataTable();
		string connstr = ConfigurationManager.ConnectionStrings[ "SQLDB" ].ConnectionString;

		
		using( SqlConnection sqlconn = new SqlConnection( connstr ) )
		{
		try
		{
		string sqlstatement = @"select top 100000 * from BILL";
		using( SqlCommand commmand = new SqlCommand( sqlstatement, sqlconn ) )
		{
		if( sqlconn.State != ConnectionState.Open )
		sqlconn.Open();

		
		SqlDataAdapter sqlda = new SqlDataAdapter( commmand );
		sqlda.Fill( dt );
		DateTime Endtime = DateTime.Now;
		Thread.Sleep( 3000 ); //延長3秒
		DateTime Totaltime = DateTime.Now;
		File.WriteAllText( @"D:\Log.txt", string.Format( "開始時間:{0} ,結束時間:{1} ,整體時間:{2}",
		Starttime.ToString(),Endtime.ToString(), Totaltime.ToString() ) );
		}
		}
		catch( Exception ex )
		{                      
		throw ex;
		}
		}           
		}        

 

 

.aspx

image

image

 

執行

imageimage

 

Log.txt

image

 

sqlProfiler

image

 

結論:

可以看到指定單向作業後真的是一去不回頭,Client端無須管(等待)Web Server工作是否執行完畢,

但Error Handel可不能不管,不然到時就不知道該Web Service執行狀況了。

最後祝大家新年快樂~2011鴻兔大展。

 

參考

WebService 類別

SoapDocumentMethodAttribute.OneWay 屬性