我正在編寫一個測試WCF服務器,並添加2個數字並等待可配置的毫秒數。多線程服務器
我寫了一個wcf客戶端。當我打開此客戶端的兩個實例時 - 在clientA上,等待值爲50秒,而在另一個客戶端B上,等待時間爲0秒。我期望當客戶端A正在運行(長進程)時,客戶端B將立即得到它的響應。
但它不工作。我一直在關注本教程WCF Concurrency
爲什麼它不適合我?
WCF服務
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFService
{
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,ConcurrencyMode = ConcurrencyMode.Multiple,UseSynchronizationContext = true)]
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,ConcurrencyMode = ConcurrencyMode.Single)]
//[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode = ConcurrencyMode.Multiple)]
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFJobsLibrary : IWCFJobsLibrary
{
public ReturnClass AddNumbers(int FirstNumber, int SecondNumber, int Delay) //Add two numbers and wait a predefined interval
{
ReturnClass myReturnClass = new ReturnClass(-1, null, null, 0);
try
{
myReturnClass.ErrorCode = 1;
myReturnClass.Result = FirstNumber + SecondNumber;
System.Threading.Thread.Sleep(Delay); // Wait
return myReturnClass;
}
catch (Exception ex)
{
myReturnClass.ErrorCode = -1;
myReturnClass.ErrorMessage = ex.ToString();
return myReturnClass;
}
}
}
}
WCF客戶
try
{
radTextBoxResult.Text = ""; // Reset Result
ServiceReference1.WCFJobsLibraryClient Client = new ServiceReference1.WCFJobsLibraryClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(radTextBoxbaseAddress.Text);
WCFClient.ServiceReference1.ReturnClass AddNumbers_Result;
AddNumbers_Result = Client.AddNumbers(int.Parse(radTextBoxFirstNumber.Text), int.Parse(radTextBoxSecondNumber.Text), int.Parse(radTextBoxDelay.Text));
if (AddNumbers_Result.ErrorCode < 0)
{
// If exception happens, it will be returned here
MessageBox.Show(AddNumbers_Result.ErrorCode.ToString() + " " + AddNumbers_Result.ErrorMessage + " " + AddNumbers_Result.ExMessage);
}
else
{
radTextBoxResult.Text = AddNumbers_Result.Result.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
App.Config中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceThrottling maxConcurrentCalls="16" maxConcurrentInstances="2147483647" maxConcurrentSessions="10" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFService.WCFJobsLibrary">
<endpoint address="" binding="wsHttpBinding" contract="WCFService.IWCFJobsLibrary">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
「然而,不工作。」發生了什麼以及預期的是什麼? – usr 2013-02-09 16:26:43
我期望的是,如果clientA的Delay值是50秒,並且我按下按鈕來添加兩個數字。然後,當我運行時,我以1秒的延遲值啓動ClientB,然後按下按鈕添加兩個數字,它在一秒鐘後得到結果。究竟發生了什麼,直到ClientA得到結果纔得到結果。 – user1438082 2013-02-09 16:39:25
開始撥打50個延遲版本的100個電話。打破服務器上的調試器。大多數線程停止在哪裏?發佈堆棧。 – usr 2013-02-09 16:49:54