WCF为什么不能并发访问?

whucv 2014-04-24 10:45:19
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);
}
}
}
...全文
894 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
无名de旅人 2014-11-03
  • 打赏
  • 举报
回复
引用 12 楼 whucv 的回复:
对于http协议,将下面的maxconnection改为你想并发的数
<configuration>
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "2" />
    </connectionManagement>
  </system.net>
</configuration>
对的,我也找到这个问题了,就是http发布,包括webservice 也这样 添加System.Net.ServicePointManager.DefaultConnectionLimit = 512; 也一样的效果,我特此过来回复一下,这个搞的我很长时间
江南小鱼 2014-10-30
  • 打赏
  • 举报
回复
<binding name="BasicHttpBindingCharge" closeTimeout="00:10:00"
          openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxBufferSize="65536000" maxReceivedMessageSize="65536000"
          textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="false"
          messageEncoding="Text">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
whucv 2014-10-23
  • 打赏
  • 举报
回复
对于http协议,将下面的maxconnection改为你想并发的数
<configuration>
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "2" />
    </connectionManagement>
  </system.net>
</configuration>
无名de旅人 2014-10-22
  • 打赏
  • 举报
回复
找到原因了,你的服务协议估计应该用的http一类,http一类都存在这个问题,改成tcp就没问题了
无名de旅人 2014-10-21
  • 打赏
  • 举报
回复
我也是这个问题,10个并发过去,结果只会两条两条返回,一直查不出什么原因。 如果服务端和客户端在一台机器就没问题,如果客户端和服务端分别在两台服务器就这样 根本不是你们说说的原因,估计你们没去去调试楼主代码,或者没有将2者分别运行在两台机器上
风之影子 2014-06-11
  • 打赏
  • 举报
回复
最大并发链接 是可以设置的, 另外客户端的调用结束后一定要注意手工关闭
superliu1122 2014-04-28
  • 打赏
  • 举报
回复
每个线程创建新的client 服务器循环里加Thread.Sleep(0);
whucv 2014-04-27
  • 打赏
  • 举报
回复
引用 3 楼 book_frank_xl 的回复:
WCF服务端默认有限流设置。最大消息 最大并发链接 最大服务实例。 这个默认的记得是10个链接 你可以在服务端服务行为里设置。 欢迎你来微软的官方WCF论坛~ <serviceBehaviors> <behavior name="myBehavior"> <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100" /> </behavior> </serviceBehaviors>
麻烦您在自己电脑上运行下程序再说。该试的方法我都试过了。
whucv 2014-04-27
  • 打赏
  • 举报
回复
引用 3 楼 book_frank_xl 的回复:
WCF服务端默认有限流设置。最大消息 最大并发链接 最大服务实例。 这个默认的记得是10个链接 你可以在服务端服务行为里设置。 欢迎你来微软的官方WCF论坛~ <serviceBehaviors> <behavior name="myBehavior"> <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100" /> </behavior> </serviceBehaviors>
引用 1 楼 zhanghb0001 的回复:
每个线程创建新的client
试过了,没用。我都把代码贴出来了,你可以在自己电脑上运行一下。
  • 打赏
  • 举报
回复
引用 1 楼 zhanghb0001 的回复:
每个线程创建新的client
可以试一下。
  • 打赏
  • 举报
回复
如果你确定java调用时可以,那么这就不是服务端的事情了,跟WCF服务没有关系了!
老徐FrankXuLei 2014-04-26
  • 打赏
  • 举报
回复
WCF服务端默认有限流设置。最大消息 最大并发链接 最大服务实例。
这个默认的记得是10个链接
你可以在服务端服务行为里设置。 欢迎你来微软的官方WCF论坛~
<serviceBehaviors>

<behavior name="myBehavior">

<serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100"

maxConcurrentInstances="100" />

</behavior>

</serviceBehaviors>
moonwrite 2014-04-25
  • 打赏
  • 举报
回复
在客户端的web.config可以设置有多少个请求,还有每个请求的超时的吧(这个一定有)~
zhanghb0001 2014-04-25
  • 打赏
  • 举报
回复
每个线程创建新的client

12,166

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧