kay.one
kay.one

Reputation: 7692

How can I get WCF to close connections automatically?

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>

Upvotes: 4

Views: 5625

Answers (3)

marc_s
marc_s

Reputation: 755157

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

Upvotes: 1

  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!

Upvotes: 2

Andr&#233; Haupt
Andr&#233; Haupt

Reputation: 3524

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

Upvotes: 1

Related Questions