2011-03-25 46 views
0

我有以下情形:WCF + asp.net +多線程

,什麼也不做,但休眠時間的WPF通過的WebRequest onject指定量的一個服務:

public class WorkerSvc : IWorkerSvc 
    { 

    #region IWorkerSvc Members 

    public void DoWork(int timeToSleep) 
    { 
     Trace.WriteLine(DateTime.Now.ToString() + "\tInside Stress Service with TimeToSleep: " + timeToSleep.ToString()); 

     if (timeToSleep == 0) 
      return; 

     Thread.Sleep(timeToSleep * 1000); 

     Trace.WriteLine(DateTime.Now.ToString() + "\tThe Thread woke up."); 

    } 

然後,一個aspx頁面(單獨的項目)在調用該服務:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tInside default stress web site page load;"); 

     using (WorkerService.WorkerSvcClient client = new StressWebSite.WorkerService.WorkerSvcClient()) 
     { 
      System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tCreated a new client of Stress WCF Service;"); 

      var rowdata = Request.QueryString["SleepValue"]; 
      System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tDetected Sleep Value in the Request: " + rowdata); 

      int realData = 0; 

      if (!string.IsNullOrEmpty(rowdata)) 
      { 
       if (int.TryParse(rowdata, out realData)) 
       { 
        System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tBefore Dowork() with SleepValue: " + realData); 
        client.DoWork(realData); 
        System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tAfter DoWork() with SleepValue: " + realData); 
        Response.Write(realData.ToString()); 
        //Response.End(); 
       } 

      } 

     } 
    }   

一個WPF形式spanws多個線程基本上一些數據發佈到aspx頁面的:

private void PerformStress() 
    { 
     WebRequest request = WebRequest.Create(string.Concat(this.txtStressPageUrl.Text, "?", "SleepValue", "=", this.txtSleepValue.Text)); 
     Trace.WriteLine("Created Web Request"); 
     request.Credentials = CredentialCache.DefaultCredentials; 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     Stream dataStream = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(dataStream); 

     string responseFromServer = reader.ReadToEnd(); 
     //return responseFromServer; 
    } 



    private void button3_Click(object sender, RoutedEventArgs e) 
    { 
     int stressThreadsCount = 0; 

     if (!int.TryParse(this.txtStressThreadCount.Text, out stressThreadsCount)) 
      MessageBox.Show("Enter number of threads to stress"); 


     ThreadStart start = delegate() 
     { 
      DispatcherOperation op = Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(PerformStress)); 

      DispatcherOperationStatus status = op.Status; 

      while (status != DispatcherOperationStatus.Completed) 
      { 
       status = op.Wait(TimeSpan.FromMilliseconds(1000)); 
       if (status == DispatcherOperationStatus.Aborted) 
       { 
        // 
       } 
      } 


     }; 

     for (int i = 0; i < stressThreadsCount; i++) 
     { 
      var t = new Thread(start); 
      //this.runningThreads.Add(t); 
      t.Start(); 
      //t.Join(); 
     } 

    } 

的問題是,它看起來像eventhough我產卵從WPF側幾個線程,張貼到aspx頁面,所有線程都以串行的方式進行處理,這意味着每個線程在等待服務返回。

我在cassiny下運行這個設置。

I 沒有指定WCF服務的行爲是單例。 請幫助識別這個問題 - 我想模擬對IIS的多個請求以及調用WCF來證明概念的頁面。

回答

1

單身人士只會影響將創建多少個對象來爲請求提供服務。但是,默認情況下,WCF服務一次只能處理一個請求。要改變這種狀況,你應該裝點

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 

服務實現欲瞭解更多信息,請看這裏:http://msdn.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx

+0

這是有趣的,但是,我認爲每一個WebRequest的將創建apsx頁面的不同實例,其輪到它會創建一個wcf的新實例嗎? – dexter 2011-03-25 20:27:29

+0

@chad,而新的頁面實例將被創建,每個只創建一個新的WCF服務請求。如果沒有上述服務行爲,這些請求將排隊並按順序處理。 – dlev 2011-03-29 17:52:07