我正在编写一个测试 WCF 服务器,其方法是添加 2 个数字并等待可配置的毫秒数。
我写了一个 wcf 客户端。当我打开此客户端的两个实例时 - 在客户端 A 上,等待值为 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());
}
应用程序配置
<?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>