How can I get WCF to automatically close connections?

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

I know that you have to clean up after yourself, but I also know that you did not need to do this using web services. Many people who are going to hit our service do not close the connection.

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

Here is my configuration

<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> 
+4
source share
3 answers

5 connections probably come from the server β€” you can determine the number of maximum open sessions, maximum simultaneous calls, and maximum server instances using the serviceThrottling server serviceThrottling .

At the same time, this will allow you to increase the number of simultaneous open sessions, I would recommend correctly clearing after myself - even if you did not have to in the old days .....

I would suggest including the use of your client proxy in the using statement, for example:

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

Update: as the commentator rightly noted, this has a share of problems, as the client may throw an exception when deleting. Thus, this may not work so well - or you need to wrap a try...catch around it to handle cases where closing the client proxy causes a problem.

See Avoid problems using instructions.


Thus, the client proxy is automatically closed and deleted when the block usage area ends, and your channel from the client to the server is freed, and the server is ready to accept another call from another client.

In addition, with wsHttpContextBinding you should check if you really need sessions that are enabled by default - the recommended best practice will be to use call requests on the server, for example. each caller creates a new server object. Sessions are a lot of new problems and potential errors, so I would try to use them only when I really need (and benefit from them) - otherwise disconnect sessions.

Mark

+1
source

Voila!

+2
source

Also check out the WCF Dynamic Client Proxy . It will automatically clear after your proxy.

+1
source

Source: https://habr.com/ru/post/1285779/


All Articles