WCF stops responding after about 10 or so calls (throttling)
Asked Answered
B

6

40

I have a WCF Service and an application with a Service Reference to it, and with the application I have a loop and in each iteration it's making a call to a method in this wcf web-service.

The problem is that after about 9 calls or so, it just stops...and if you hit Pause button of VS, you will see that it's stuck on the line where it makes the call.

After some time waiting for it, this TimeoutException is thrown:

The request channel timed out while waiting for a reply after 00:00:59.9970000. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.


I researched a bit on this, and found some solutions that involved editing the app.config in the application, and here are excerpts of it:

<serviceBehaviors>
    <behavior name="ThrottlingIssue">
        <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" />
    </behavior>
</serviceBehaviors>

.

<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
 maxArrayLength="2147483647" 
 maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 

Then, after I stop debugging, after a couple of minutes, an error message pops up telling me that a Catastrophic failure has occurred.

How can I fix this problem? I did not have this issue when I was working with a normal Web Service.


For reference, here is the whole app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ThrottlingIssue">
                    <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IDBInteractionGateway" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:28918/DBInteractionGateway.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDBInteractionGateway"
                contract="DBInteraction.IDBInteractionGateway" name="WSHttpBinding_IDBInteractionGateway">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

[Update] Solution:

Apparently, after each request you have to Close the connection...I am now closing the connection after each request and it's working like a charm.

Although what I still can't understand is that in my app.config, I set my maxConcurrentCalls and maxConcurrentSessions to 500, and yet, I can only make 10. Anyone has any answer for that one? (maybe I have something wrong in my app.config posted above)

The answer for the above question (now dashed) is because I was editing the client app.config, not the service config file (web.config)

Blakely answered 11/4, 2009 at 1:14 Comment(3)
Had been bashing my head in over the last two days trying to figure this one out, thanks!Satisfaction
Wish I could upvote this question more than once.Colander
i am on exact same situation... could not help your post to fix my problem, will you have a look at this question? #10306171Vaas
A
27

The default number of allowed concurrent connections is 10.
Most likely your client is not closing the connections.

To increase the number of concurrent calls, you will have to add your behavior to the service configuration, not the client.

Able answered 11/4, 2009 at 2:0 Comment(9)
In my app.config, I set my maxConcurrentCalls and maxConcurrentSessions to 500, and yet, I can only make 10. Is there something wrong with my app.config?Blakely
Did you set the serviceBehavior on the service element to use the behavior configuration.Able
It looks like you posted the client app.config ? - you will need to change the service config..Able
But I don't have an app.config file in my WCF project. Should there be one?Blakely
How are you hosting it? .. if in IIS, it could be in web.config.Able
Ah yes, it is infact in the web.config. Fixed it now. Thanks for your help.Blakely
The term 'connection' is a little confusing here, but I almost can't wait to update all my code tomorrow. This was driving me crazy during testing.Blaisdell
allowed concurrent connections is not enough. he still need to close connections.Marcenemarcescent
What if it is webget operation, for example when "client" is browser requesting image server with WCF.? #40378159Hermann
M
3

A call to clientservice.close() will solve the problem.

Moureaux answered 2/8, 2011 at 15:43 Comment(1)
Yea, that's what I mentioned in the Solution [Update] to the question.Blakely
B
1

I ran into this problem this week and I wasn't able to quite figure out what was going on. I actually did change my call to my service to Dispose() the service client, but it didn't seem to have any effect. Apparently, there was another service call lurking somewhere.

What may be interesting to note is what made me decide that this was not the problem: this limit is not related to the actual number of socket connections to the webservice. When you hit the maxConcurrentSessions limit, there is still only one actual socket connection. I was checking this with netstat, which brought me to the wrong conclusion. So, don't confuse sessions with sockets.

We have interfaces defined for all our WCF services, so I'm planning to adapt this pattern in my code now:

IMyService service = new MyServiceClient();
using (service as IDisposable)
{
    service.MyServiceMethod();
}

What's also interesting, is that the problem did not occur for me when the services (and website) were hosted on IIS. The configuration is (almost) identical, yet I could not reproduce this behavior on that machine. I guess that's a good thing :)

@ John Saunders (about variable assignment in using):

I usually do put the variable assignment in the using statement. But the IMyService that's generated is not implicitly convertible to IDisposable. If you really wanted the assignment in there, I suppose the alternative would be:

IService service;
using ((service = new ServiceClient()) as IDisposable)
{
}

That still leaves the problem of the variable scope being wrong though. The reference to IService service is unusable, but still in scope. So this would be better in that respect:

using (IDisposable serviceDisposable = new ServiceClient())
{
     IService service = (IService)serviceDisposable;
}

That requires me to introduce an extra variable name though. *Meh*

Blaisdell answered 5/8, 2009 at 18:29 Comment(6)
Why not put the assignment into the using block as well?Avent
Also, see iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx.Avent
I normally do, but using (IMyService service = new MyServiceClient()) does not work (type used in a using statement must be implicitly convertible to 'System.IDisposable'). Doing both assignment and casting inside the using seemed a little messy to me.Blaisdell
The delegate solution in that article is sort of elegant, but I'm not sure if my code snippet above warrants such a solution. It's not very likely that that execution will be interrupted in between those lines, unless your app dies?Blaisdell
using (var service = new ServiceClient())Avent
That works yes, but I like to be more explicit about my types, if it's not immediately evident from the code already.Blaisdell
E
1

This can be solved by creating singleton class as interface between web service reference and application. Then it will create only one instance of service reference.

class ServiceInterface
{
     private static ServiceInterface  _instance;
     private ServiceClient _service = new ServiceClient ;
     private ServiceInterface()
     {
       //Prevent accessing default constructor
     }

     public static ServiceInterface GetInstance()
     {

     if(_instance == null)

     {

      _instance = new ServiceInterface();

    }

        return _instance;



 }


   // You can add your functions to access web service here

    Public int PerformTask()
    {
         return _service.PerformTask();
    }
}
Evasion answered 25/6, 2010 at 15:39 Comment(1)
It's more robust to create a client on each request, than to keep the singleton around. I tried this method, and if there are failures on the client side (ie Status == Fault), then new requests fail.Shallow
S
0

Can you configure tracing and run the loop? It may be that the channel is becoming faulted and causing the client to time out.

Smidgen answered 11/4, 2009 at 1:49 Comment(0)
E
0

After pulling my hairs over a much similar problem that was not solved by either Close() or Dispose() I would like to add a simple solution that made my day, namely by increasing the ServicePointManager.DefaultConnectionLimit which is by default 2.

"The DefaultConnectionLimit property sets the default maximum number of concurrent connections that the ServicePointManager object assigns to the ConnectionLimit property when creating ServicePoint objects."

In my case my application successfully connected to my remote service 2 times, on the third attempt it simply did not try to connect to the service. Instead it waited for a while before timing out with the same error message as in the question above. Increasing DefaultConnectionLimit resolved this. To add to the frustration this behavior was somewhat random - in one case of 10 the webservice was invoked successfully multiple (>2) times.

The solution originates and are further discussed these two threads: wcf-timeout-exception-detailed-investigation and wcf-service-throttling. solved my issue.

Exegete answered 18/8, 2011 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.