多线程服务器
本文关键字:服务器 多线程 | 更新日期: 2023-09-27 17:58:23
我正在编写一个测试WCF服务器,该服务器具有一个添加2个数字并等待可配置毫秒数的方法。
我写了一个wcf客户。当我打开该客户端的两个实例时——在客户端A上,等待值为50秒,在另一个客户端B上,等待为0秒。我希望当客户端A运行(长进程)时,客户端B会立即得到响应。
然而,它不起作用。我一直在学习这个教程WCF并发
为什么它对我不起作用?
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>
问题可能是InstanceContextMode=InstanceContextMode.PerSession设置导致您的两个调用共享同一会话。尝试InstanceContextMode=InstanceContextMode.PerCall.
这两个调用都使用服务的一个实例,但如果第一个调用停止了50秒,那么第二个调用处理将等待,直到线程恢复工作并为第一个调用提供服务。
问题是我在主线程上的winform应用程序中启动服务。您需要在一个单独的线程上启动WCF,如下所示。
C#代码
private void radButtonStartWCFService_Click(object sender, EventArgs e)
{
try
{
Thread Trd = new Thread(() => StartWCFServer());
Trd.IsBackground = true;
Trd.Start();
radButtonStartWCFService.Enabled = false;
radButtonStartWCFService.Text = "WCF Server Started";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void StartWCFServer()
{
try
{
sHost = new ServiceHost(typeof(WCFService.WCFJobsLibrary));
sHost.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}