【发布时间】:2018-08-25 05:57:51
【问题描述】:
在控制台应用程序和同一个客户端中有一个简单的 WCF 服务。客户端同时(在不同的线程中)向服务器发送 N 个请求。预计处理并发请求的服务器会同时分配多个线程,并在第二次停机后(由 Thread.Sleep (1000) 专门制作)同时将答案返回给客户端,但这不会发生。尽管 ServiceBehavior 属性设置为 ConcurrencyMode.Multiple,但服务在一个线程中处理所有请求(这在屏幕截图中的 ThreadId 中可以看到)。
服务应用代码:
namespace Server
{
using System;
using System.ServiceModel;
using System.Threading;
[ServiceContract]
public interface IServer
{
[OperationContract]
int GetResult(int value);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single)]
public class Server: IServer
{
public int GetResult(int value)
{
Console.WriteLine("In: {0}, Time: {1}, ThreadId: {2}",
value, DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
Console.WriteLine("Out: {0}, Time: {1}, ThreadId: {2}",
value, DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId);
return value;
}
}
class Program
{
static void Main()
{
var host = new ServiceHost(new Server());
host.Open();
Console.WriteLine("Service started");
Console.ReadLine();
}
}
}
客户端应用代码:
using System;
namespace Server
{
using System.ServiceModel;
[ServiceContract]
public interface IServer
{
[OperationContract]
int GetResult(int value);
}
}
namespace Client
{
using System.ServiceModel;
using System.Threading;
using Server;
class Program
{
static object lockObj = new object();
static void Main()
{
ChannelFactory<IServer> factory = new ChannelFactory<IServer>("defaultEndPoint");
IServer channel = factory.CreateChannel();
const int threadCount = 6;
int value = 0;
for (int i = 0; i < threadCount; i++)
{
new Thread(state =>
{
int n;
lock (lockObj)
{
value++;
n = value;
}
Console.WriteLine("Send value = {0}, Time = {1}", n, DateTime.Now.TimeOfDay);
n = channel.GetResult(n);
Console.WriteLine("Response value = {0}, Time = {1}", n, DateTime.Now.TimeOfDay);
}).Start();
}
Console.ReadLine();
}
}
}
服务配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Server.Server" behaviorConfiguration="DefaultBehavior">
<endpoint contract="Server.IServer" binding="basicHttpBinding" address=""/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:7803/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
客户端的控制台输出:
发送值 = 1,时间 = 11:20:13.1335555
发送值 = 3,时间 = 11:20:13.1335555
发送值 = 2,时间 = 11:20:13.1335555
发送值 = 4,时间 = 11:20:13.1335555
发送值 = 5,时间 = 11:20:13.1335555
发送值 = 6,时间 = 11:20:13.1335555
响应值 = 3,时间 = 11:20:14.6191583
响应值 = 1,时间 = 11:20:15.6184362
响应值 = 2,时间 = 11:20:16.6342291
响应值 = 4,时间 = 11:20:17.6497805
响应值 = 5,时间 = 11:20:18.6657260
响应值 = 6,时间 = 11:20:19.6820159
服务端的控制台输出:
在:3,时间:11:20:13.5030783,ThreadId:12
输出:3,时间:11:20:14.5184547,ThreadId:12
在:1,时间:11:20:14.6035310,ThreadId:12
输出:1,时间:11:20:15.6184362,ThreadId:12
在:2,时间:11:20:15.6184362,ThreadId:12
输出:2,时间:11:20:16.6342291,ThreadId:12
在:4,时间:11:20:16.6342291,ThreadId:12
输出:4,时间:11:20:17.6497805,ThreadId:12
在:5,时间:11:20:17.6497805,ThreadId:12
输出:5,时间:11:20:18.6657260,ThreadId:12
在:6,时间:11:20:18.6657260,ThreadId:12
输出:6,时间:11:20:19.6820159,ThreadId:12
【问题讨论】:
标签: c# .net multithreading wcf