为什么我的WCF服务一次只能处理3个并行请求? [英] Why is my WCF service only processing 3 parallel requests at a time?

查看:169
本文介绍了为什么我的WCF服务一次只能处理3个并行请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,它做的非常简单.我只是触发了对基本WCF Service4请求.

I have a WPF application doing something very simple. I just have it firing off 4 requests to a basic 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).

My service is doing the classic 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秒后,我希望所有四个请求都返回.好吧,实际上发生的是所有四个请求都通过网络发送,并且前三个请求将返回,然后最后一个请求将在这些请求之后的3秒后返回.

I expect 4 request to get fired off, and 3 seconds later, I want all four requests to return. Well, what's actually happening is all four requests are being sent over the wire, and the first three requests will return, then the last request will return 3 seconds after those.

节流

起初我以为这是我的问题.好吧,我在web.config中添加了<serviceThrottling maxConcurrentCalls="10"/> int,并且没有任何区别.因此,然后我阅读了我的代理可能达到的连接限制,因此我在WPF应用程序中添加了ServicePointManager.DefaultConnectionLimit=10;.

At first I thought this was my issue. Well, I added <serviceThrottling maxConcurrentCalls="10"/> int my web.config and it didn't make any difference. So, then I read where it could be a connection limit that I'm reaching with my proxy, so I added ServicePointManager.DefaultConnectionLimit=10; to my WPF application.

饥饿的线程

然后,我不断遇到线程饥饿的情况.因此,作为一个测试,我尝试将最小线程数设置为20,以确保我有足够的启动时间来处理4请求.我将此添加到我的WPF应用程序ThreadPool.SetMinThreads(20, 20);中.因此,为了确认是否有大量工作线程可用于处理请求,我决定让我的服务调用在流程开始时返回可用线程的数量.每个请求都返回1920.

Then, I kept coming across thread starving. So just as a test, I tried to set the minimum thread count to 20 to make sure that I had enough spun up to handle the 4 requests. I added this to my WPF application ThreadPool.SetMinThreads(20, 20);. So to confirm that there were plenty of worker threads available to handle the requests, I decided to have my service call return the number of available threads at the beginning of the process. Every request returns 19 or 20.

使用Fiddler,我可以确认所有4请求均已从WPF(客户端)应用程序通过电线发送.现在,我的Wcf使用的是basicHttpBinding,因此它以Per Call实例模式运行,因此并发模式无关紧要,但是为了安全起见,我添加了[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)].

Using Fiddler, I can confirm that all 4 requests have been sent across the wire from the WPF (client) application. Now, my Wcf is using basicHttpBinding, so it's running in Per Call instance mode so concurrency mode shouldn't matter, but I've added [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)] just to be safe.

我已经使用System.Diagnostics.XmlWriterTraceListener完成了一些堆栈跟踪,可以确认同时(几乎)同时记录了3请求,然后在3秒后记录了第四个请求.

I've done some stack tracing using the System.Diagnostics.XmlWriterTraceListener and I can confirm that I get 3 requests logged at the same time (nearly), then the fourth request gets logged 3 seconds later.

Wcf托管在IIS中.如果需要更多信息,请告诉我,我会尽快添加.

The Wcf is hosted in IIS. If anymore information is needed, please let me know and I'll add it as soon as possible.

根据要求,这是我的WCF界面.

Per requested, here is my WCF interface.

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);
    }
}

推荐答案

经过大量研究,

After a lot of research, this is the only thing that I have found that may explain this behavior. I'm running IIS 8 on Windows 10. Though I can't find the limit for my exact version combination, I think this could potentially be the issue.

Microsoft似乎会限制同时请求的数量.对于某些基本版本,限制为3.我似乎无法在任何地方更改这些设置,因此,如果您知道怎么做,请为遇到此问题的其他人发布另一个答案.

It seems that Microsoft would put a limit on the number of simultaneous requests. For some of the basic versions, 3 would be the limit. I can't seem to alter these settings anywhere, so if you know how, please post another answer for someone else who comes across this.

这篇关于为什么我的WCF服务一次只能处理3个并行请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆