我有一个WPF应用程序做一些非常简单的事情。我只是让它点火
4
基本要求
WCF Service
.
private async Task MultipleWcfCalls()
{
ServiceReference.Service1Client _proxy = new ServiceReference.Service1Client();
_proxy.Open();
var t1 = _proxy.GetDataAsync(0);
var t2 = _proxy.GetDataAsync(0);
var t3 = _proxy.GetDataAsync(0);
var t4 = _proxy.GetDataAsync(0);
await Task.WhenAll(t1, t2, t3, t4);
}
我的服务是做经典
Task.Delay(3000)
.
public async Task<string> GetDataAsync(int value)
{
int other = 0;
int workerThreadsAvail = 0;
ThreadPool.GetMinThreads(out workerThreadsAvail, out other);
await Task.Delay(3000);
return workerThreadsAvail.ToString();
}
我希望如此
4
要求被解雇,以及
3
几秒钟后,我希望所有四个请求都返回。实际上,这四个请求都是通过网络发送的,前三个请求将返回,最后一个请求将返回
三
几秒钟后。
节流
一开始我以为这是我的问题。嗯,我补充道
<serviceThrottling maxConcurrentCalls="10"/>
int我的
web.config
ServicePointManager.DefaultConnectionLimit=10;
我的WPF应用程序。
缺线
然后,我总是挨饿地碰到线。所以作为一个测试,我尝试将最小线程数设置为
20
以确保我有足够的精力来处理
4
请求。我把这个添加到我的WPF应用程序中
ThreadPool.SetMinThreads(20, 20);
19
或
20
.
4
请求已经从WPF(客户机)应用程序通过线路发送。现在,我的Wcf正在使用
basicHttpBinding
Per Call
实例模式所以并发模式应该不重要,但是我添加了
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
只是为了安全。
我用
System.Diagnostics.XmlWriterTraceListener
我可以确认我得到
三
同时记录请求(几乎),然后第四个请求在3秒后被记录。
IIS
. 如果需要更多信息,请告诉我,我会尽快补充。
根据我的接口请求。
namespace SampleWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
System.Threading.Tasks.Task<string> GetData(int value);
}
}