wcf service long httpwebrequest wait causes queuing of subsequent requests - c#

I have a WCF service that in functionA makes an HttpWebRequest call to functionX in an external service. Originally the timeout on this httpwebrequest was set to 5 minutes.
Recently, the external service has been taking longer than 5 minutes to respond (which I am ok with). So I bumped the httpWebRequest.timeout up to 10 minutes.
Meanwhile the wcf service should be able to process other incoming requests (to functionB, functionC, etc). What I'm experiencing now is that if functionX takes longer than ~5 minutes to respond (and thus functionA takes longer than 5 minutes to complete), subsequent requests to functionB in my wcf service are queued / do not process until functionA completes.
In the end everything completes properly, but I don't see why functionB is affected by the waiting that is happening over in functionA.
Forgive me if that is hard to follow. It is a strange and I'm having trouble wrapping my head around how these pieces are related.

You must decorate your WCF Service class with following attribute
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] // The service instance is multi-threaded.
public class Service1
{
// ...
}
I assume your concurrency mode is set to Single defined as follows by Microsoft.
"The service instance is single-threaded and does not accept reentrant calls.
If the System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode property is System.ServiceModel.InstanceContextMode.Single, and additional messages arrive while the instance services a call, these messages must wait until the service is available or until the messages time out."

i had a same problem. i hosted my service in IIS. after little search i found out its because of maxconnection limit in web config. i added this line in to my web.config and the problem solved:
<system.net>
<connectionManagement>
<add address="*" maxconnection="1000"/>
</connectionManagement>
</system.net>
by default maxconnection value is 2.
but this is one of the many reasons. you should monitor your server requests in order to find out the exact reason.

Related

WCF - Combination between multiple requests from same client and requests from other client

We've had some unexpected behavior in our WCF service(s) in production. This is the (simplified) situation we're able to simulate:
We have a WCF service that retrieves some stuff from
the database.
Say there is a problem with the database, causing the SQL operation to timeout.
Extra info: The Instancecontexmode of the service is set to PerCall and ConcurrencyMode is Single.
If client 1 performs a request to the service and client 2 performs a request, we see (ie. in the Worker process requests in IIS) that these are threated concurrently and each take approx 30 seconds (because of the default SQL timeout).
However because the timeouts between client and server where not correct, we could actually perform a second request from client 1 before its initial request was handled. So then we could have:
Request 1 from client 1
Request 2 from client 1 (approx 5 seconds after first request)
Request 1 from client 2 (approx same time as request 2 from client 1)
Now the request 1 from client 1 takes approx. 30 seconds, like before. The request 2 from client 1, takes a bit less than 60 seconds. That is something that can be expected. But what is unexpected for us is that the request from client 2 also takes more or less the same time as request 2 from client 1. Where we expected it to take about 30 seconds. It seems to us that processing the call from client 2 only starts when processing of the second request from client 1 starts.
Is there someone that can explain this behavior? And what we could do to avoid/improve this?
Note: we know that we have to make sure our timeouts are 'in sync' between the client and server but our actual architecture is a lot more complex than sketched above. We will perform some optimizations, but even then we could have this situation...

Web service concurrent calls processing

I've faced with the next issue related to web service request processing:
Preamble
I have
Web api service hosted on IIS 7.0 on local machine
Test harness console application on the same machine
and i'm trying to simulate web service load by hitting one with requests generated via test harness app.
Test harness core code:
static int HitsCount = 40;
static async void PerformHitting()
{
{
await Task.WhenAll(ParallelEnumerable.Range(0, HitsCount)
.Select(_ => HitAsync())
.WithDegreeOfParallelism(HitsCount));
}
}
static async Task HitAsync()
{
// some logging skipped here
...
await new HttpClient().GetAsync(TargetUrl, HttpCompletionOption.ResponseHeadersRead);
}
Expectation
Logging shows that all HitAsync() calls are made simultaneously: each hit via HttpClients had started in
[0s; 0.1s] time frame (timings are roughly rounded here and below). Hence, I'm expecting to catch all these requests in approximately the same time frame on web service side.
Reality
But logging on the service side shows that requests grouped in bunches 8-12 request each and service catches these bunches with ~1 second interval. I mean:
[0s, 0.3s] <- requests #0-#10
[1.2s, 1.6s] <- requests #10-#20
...
[4.1s, 4.5s] <- request #30-#40
And i'm getting really long execution time for any significant HitCount values.
Question
I suspect some kind of built-in service throttling mechanism or framework built-in concurrent connections limitation. Only I found related to such guesstimate is that, but i didn't get any success trying soulutions from there.
Any ideas what is the issue?
Thanks.
By default, HTTP requests on ASP.NET are limited to 12 times the number of cores. I recommend setting ServicePointManager.DefaultConnectionLimit to int.MaxValue.
Well, the root of the problems lies in the IIS + Windows 7 concurrent requests handling limit (some info about such limits here. Moving service out to the machine with Windows Server kicked out the problem.

Max number of concurrent HttpWebRequests remove throttling

We are attempting to submit a large number of simultaneous web requests using HttpWebRequest for purposes of stress testing a server.
For this example, the web service method being tested simply pauses for 30 seconds and then returns allowing for queue depth and simultaneous request testing.
The client log shows 200 calls being queued up successfully in parallel within ~1 second using multiple calls to:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.BeginGetResponse();
Fiddler shows the first ~80 being sent to server simultaneously, then before any initial responses have come back, Fiddler shows the remaining ~120 requests being added at about 1/second.
Following the suggestions from here have allowed an increase of the effective limit from about 10 to about 80. I wish to remove the next barrier without resorting to multiple machines/processes. Any ideas?
// Please excuse the blind setting of these values to find the other problem.
// These were set much higher but did not help
ThreadPool.SetMinThreads(30, 15);
ThreadPool.SetMaxThreads(30, 15);
System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
and in App.Config
<system.net>
<connectionManagement>
<clear/>
</connectionManagement>
</system.net>
Another point is that when a large number of requests are performed and complete before the simple delay test, then the delay test is able to achieve 80 or so simultaneous. Starting in "cold" usually limits it to about 10 or so.

WCF ConcurrencyMode.Multiple

I dont know how to check if im right or wrong so your help will be great.
A. From my understanding, IsOneWay=true = the client doesnt want to wait for the method to end. so the service will execute this method when he want. But, in some cases, does the service will use multi-threading to execute the method?
B. when I use ConcurrencyMode.Multiple, what is the diffrence between using IsOneWay=true & IsOneWay=false.
ConcurrencyMode and Messaging Pattern are not so directly related.
IsOneWay affects how Client and Server interact.
The ConcurrencyMode is a server-side issue, the Client is not aware of this setting.
From: http://msdn.microsoft.com/en-us/library/ms751496.aspx
HTTP is, by definition, a request/response protocol; when a request is
made, a response is returned. This is true even for a one-way service
operation that is exposed over HTTP. When the operation is called, the
service returns an HTTP status code of 202 before the service
operation has executed. This status code means that the request has
been accepted for processing, but the processing has not yet been
completed. The client that called the operation blocks until it
receives the 202 response from the service. This can cause some
unexpected behavior when multiple one-way messages are sent using a
binding that is configured to use sessions. The wsHttpBinding binding
used in this sample is configured to use sessions by default to
establish a security context. By default, messages in a session are
guaranteed to arrive in the order in which they are sent. Because of
this, when the second message in a session is sent, it is not
processed until the first message has been processed. The result of
this is that the client does not receive the 202 response for a
message until the processing of the previous message has been
completed. The client therefore appears to block on each subsequent
operation call. To avoid this behavior, this sample configures the
runtime to dispatch messages concurrently to distinct instances for
processing. The sample sets InstanceContextMode to PerCall so that
each message can be processed by a different instance. ConcurrencyMode
is set to Multiple to allow more than one thread to dispatch messages
at a time.

Increasing timeout of ASMX webservices

I am running an ASMX webservice on IIS 7. The client is a silverlight application. I am issuing several asynchronous file requests to the webservice and processing them on AsyncCompleted. Sometime after all the async requests have been issued, I get the following timeout message. How can the timeout be increased.
I tried adding the following in the web.config of the ASP.NET project that is hosting my silverlight application.
<httpRuntime executionTimeout="12000"/>
(This timeout is not happening when I do the requests synchronously, but synchronous operations are very slow.)
The HTTP request to 'http://localhost:5080/Service1.asmx' has exceeded the
allotted timeout.
See these sources:
Timeout error in WCF
Silverlight Timeout Issues from WCF… exceeded the allotted timeout.
Because this is the silverlight application i am in a doubt if my suggestion will work.
Just try, to create the object of the webservice and then increase the time out value to what ever seconds you want.
Below code may be helpful
ConsoleApplicationFor2Groups.AdServices.ADService adser = new ADService();
adser.Timeout = 100000; // this time is in milliseconds
Just let me know if this works.

Categories