views:

703

answers:

3

When I create a WCF application using the built-in Visual Studio templates and try calling it while in a loop, only 5 requests get through. The service then stops responding. The only way that I can get around this is to close the connections after each call.

I know that you are supposed to clean up after yourself, but I also know that you didn't have to do this with web services. A lot of the people who are going to be hitting our service won't close their connection.

Is there a way to get the same behavior with WCF?

Here is my config

<system.serviceModel>
    <services>
      <service name="WorkflowLibrary1.Workflow1" behaviorConfiguration="WorkflowLibrary1.Workflow1.Service1Behavior">
        <endpoint address="" binding="wsHttpContextBinding" contract="WcfServiceLibrary1.IService1"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WorkflowLibrary1.Workflow1.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
+1  A: 

The 5 connections probably comes from the server - you can define the number of maximum open sessions, max concurrent calls, and max server instances by means of the servers serviceThrottling behavior.

At the same time, will this will allow you to increase the number of concurrently open sessions, I would still recommend to properly clean up after yourself - even if you didn't have to in the olden days.....

I would suggest wrapping the use of your client proxy into a using statement like so:

using(ClientProxy proxy = new ClientProxy())
{
   // go ahead, call your service methods
}

Update: as a commentor has rightfully pointed out, this has it's share of problems, since the client might throw an exception upon being disposed. So this might not really work all that well - or you need to wrap a try...catch around it to handle those cases where the closing of the client proxy causes an issue.

See Avoiding Problems with the Using Statement


That way, the client proxy is automatically closed and disposed of when the scope of the using block ends, and your channel from the client to the server is released and the server is ready to receive another call from another client.

Also with the wsHttpContextBinding you should check whether you really need the sessions that are on by default - the recommended best practice would be to use per-call instancing on the server, e.g. each caller instantiates a new server object. Sessions introduce a whole slew of new problems and potential pitfalls, so I would try to only use them when I really really have to (and get a benefit from it) - otherwise turn off sessions.

Marc

marc_s
I wouldn't put proxy in the using statement, because WCF is broken and dispose can throw. Use try/catch/finally/ and in finally use try/catch to close the channel and if it throws, to abort it.
Krzysztof Koźmic
let me add more detail, when and if I use all the connections and not close them after i'm done, if i go to the server and look at the perfmon for the asp.net application, i can see that there is a single request set as processing and no more reuqests are able to get through, I haven't found any way to make the server accept more request other than resetiis. I just find it hard to believe that WCF would make the server so volunarable to bad practices on the client side. based on this a single client would be able to DOS your server without even trying.
Keivan
Kay: this might be true - but if you have five clients that have connected, established a session, and have NOT been closed properly, then you'll have five sessions with the server open, and that's the default limit on the server throttling.
marc_s
This answer is INCORRECT - read this:Avoiding Problems with the Using Statementhttp://msdn.microsoft.com/en-us/library/aa355056.aspx
Wagner Danda da Silva
+1  A: 

Also have a look at the WCF Dynamic Client Proxy. It'll automatically clean-up after your proxy.

Andy
+1  A: 
  1. Read this first -> Avoiding Problems with the Using Statement: http://msdn.microsoft.com/en-us/library/aa355056.aspx

  2. If you want a more robust solution than provided in by the link above, I recommend this: http://bloggingabout.net/blogs/erwyn/archive/2006/12/09/WCF-Service-Proxy-Helper.aspx (code) http://bloggingabout.net/blogs/erwyn/archive/2007/01/30/usage-of-the-wcf-serviceproxyhelper.aspx (usage/samples)

Voila!

Wagner Danda da Silva