C.J.
C.J.

Reputation: 16091

How to change basicHttpBinding sendTimeout at runtime?

I have a small, basic working example of using WCF to get two applications to talk to each other. My client app, that listens to the server, has XML in the app.config file that configures my settings. One setting that is hard-coded at compile time is the sendTimeout settings that is buried under the basicHttpBinding setting. For example:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IScriptRunHost" closeTimeout="00:05:00"
                    openTimeout="00:05:00" receiveTimeout="00:05:00" sendTimeout="00:00:15"

I would like to be able to set the sendTimeout property at runtime (using c#). However being so new to WCF I don't know where to start?

Upvotes: 3

Views: 21389

Answers (1)

ms87
ms87

Reputation: 17492

You can do whatever you do in your config file, in code. You can set your timeouts or various configuration details dynamically by creating a new client proxy and assigning the desired binding configurations to it at run time:

ServiceClient _client = new ServiceClient(new BasicHttpBinding { SendTimeout = new TimeSpan(2, 0, 0) },new EndpointAddress("http://localhost:8089/MyService.svc"));

or:

BasicHttpBinding myBinding = new BasicHttpBinding();
                 myBinding.OpenTimeout = new TimeSpan(2, 0, 0);
                 myBinding.CloseTimeout = new TimeSpan(2, 0, 0);
                 myBinding.SendTimeout = new TimeSpan(2, 0, 0);

ServiceClient _client = new ServiceClient();
              _client.Endpoint.Binding = myBinding;

But as you can probably infer by glancing at the code, if you want to change your timeout values, your service endpoint, or pretty much any of your binding configurations at run time, you'd have to tear down the previous client proxy and dispose of it and use the new one you created, obviously this has some undesired effect as your clients will momentarily be disconnected from your service, so bear that in mind. You could also define 2 or 3 (or as many as you wish) binding configurations in your config file, and create a new client and configure it to use that binding, which is almost identical to doing it in code. Even this way you'd have to instantiate a new client proxy to be able to use a different binding configuration.

Upvotes: 8

Related Questions