WCF为什么不能并发访问?

whucv whucv 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
C#客户端调用只两条线程,不知道为什么?另有用Java调用服务,可以并行访问。
接口定义
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
[ServiceContract]
public interface IService
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: 在此添加您的服务操作
}

// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

服务定义


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、服务和配置文件中的类名“Service”。
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
public class Service : IService
{
public string GetData(int value)
{
DateTime startTime = DateTime.Now;
Int64 n = 30000000;
while (n > 0)
{
Math.Pow(33333333333333333.33333, 23333333);
n--;
}
DateTime endTime = DateTime.Now;
string msg = string.Format("服务端耗时:{2}ms,start:{0}_{1}", startTime.ToString("HH:mm:ss.ffffff"), endTime.ToString("HH:mm:ss.ffffff"),
(endTime - startTime).TotalMilliseconds);
Console.WriteLine(msg);
return string.Format("Server:{0},{1}", value,msg);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{

class Program
{
static Stopwatch sw = new Stopwatch();
static GTService.Service client = new GTService.Service();
class AAA
{
public int Value = -1;
public AAA(int value)
{
Value = value;
}
}
static void Main(string[] args)
{
sw.Start();
ThreadPool.SetMinThreads(25, 25);
for (int i = 0; i < 20; i++)
{
AAA a = new AAA(i);
System.Threading.ThreadPool.QueueUserWorkItem(DoWork,a);
}
Console.ReadKey();
}
static void DoWork(Object valuei)
{
AAA a = valuei as AAA;
int value = a.Value;
DateTime startTime = DateTime.Now;

string serverMsg=client.GetData(value, true);
DateTime endTime = DateTime.Now;
string clientMsg = string.Format("客户端耗时:{2}ms,start:{0}_{1},",
startTime.ToString("HH:mm:ss.ffffff"), endTime.ToString("HH:mm:ss.ffffff"),
(endTime - startTime).TotalMilliseconds);
string msg = string.Format("累计时长:{3}ms,client:{0},{1}__{2}",
value, clientMsg, serverMsg,sw.ElapsedMilliseconds);
Console.WriteLine(msg);
Debug.WriteLine(msg);
}
}
}
...全文
评论 14
收藏 已收藏
分享
返回顶部
whucv
Web Services 12,164 成员 16,322 内容