11

I'm using WCF in communication between a server and client (both written in C#).

In release-mode, the timouts should be set to ~20 seconds, but in debug mode I want to set them to a higher value so that I can debug/step in my code without the timeout occurring.

I know that I can change the timeouts by modifying the app.config file. However, I've got two different bindings and 4 time out values in each so I would have to change in several places, and its easy to forget.

To solve this, I would like to have a small #if DEBUG-section in my code which programmatically changes the timeout values to 1 hour.

I tried to use the following code to do this:

Configuration configuration = 
       ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = 
       ServiceModelSectionGroup.GetSectionGroup(configuration); 

BindingsSection bindings = serviceModel.Bindings;

foreach (var configuredBinding in bindings.WSHttpBinding.ConfiguredBindings)
{
 configuredBinding.CloseTimeout = new TimeSpan(0, 30, 0);
 configuredBinding.OpenTimeout = new TimeSpan(0, 30, 0);

but the *Timeout properties are readonly so I get a compilation error.

I'm not fond of the idea of creating bindings from scratch programmatically. If I change some of the attributes in the app.config, I have to remember to do the same change in the code to make sure that the debug-behavior is similar to the release-behavior (except for the timeouts..)

How to handle this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Martin
  • 113
  • 1
  • 1
  • 4

2 Answers2

16

You could do the following:

  • create the binding and the endpoint in code
  • set the timeouts on the binding instance
  • then create your client proxy using those two elements

Something like:

BasicHttpBinding myBinding = new BasicHttpBinding("ConfigName");
myBinding.CloseTimeout = .......
myBinding.OpenTimeout = .......
myBinding.ReceiveTimeout = .......
myBinding.SendTimeout = .......

EndpointAddress myEndpoint = new EndpointAddress("http://server:8181/yourservice");

YourServiceClient proxy = new YourServiceClient(myBinding, myEndpoint);

That way, you can leverage the basic config when describing binding timeouts and yet you can tweak the settings you want and create your client proxy from it.

Schultz9999
  • 8,717
  • 8
  • 48
  • 87
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
3

You can create a second binding in the web.config and set a longer sendTimeout.

        if (debug)
        {
            proxy =  new MyClient("WSHttpBinding_MyLocal");
        }
        else
        {
            proxy = new MyClient("WSHttpBinding_MyDev");
        }

        <wsHttpBinding>
            <binding name="WSHttpBinding_MyLocal" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:20:00"

...

John H
  • 31
  • 1
  • In a `Client`, the only timeout most people will ever need is the `sendTimeout` https://social.msdn.microsoft.com/Forums/vstudio/en-US/84551e45-19a2-4d0d-bcc0-516a4041943d/explaination-of-different-timeout-types – Serj Sagan Nov 13 '15 at 03:27