问题
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);
}
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();
}
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.
Throttling
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.
Thread Starving
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
.
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.
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.
The Wcf is hosted in IIS
. If anymore information is needed, please let me know and I'll add it as soon as possible.
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);
}
}
回答1:
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.
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.
来源:https://stackoverflow.com/questions/51796836/why-is-my-wcf-service-only-processing-3-parallel-requests-at-a-time