It's occasionally useful to be able to replace the binding being used for an existing service host. For example, if the binding was loaded from configuration, you may then want to have your code examine the binding and modify some of the settings. This lets you use the configuration system without any extensions, while still having control over the settings used from the configuration file.
Access to the service binding is available through the service description. The binding for an endpoint can be changed as long as you haven't started using the service by opening it. This example shows picking out the endpoint to modify using the contract type. There are several other options you can use to identify an endpoint.
ServiceEndpoint endpoint = host.Description.Endpoints.Find(typeof(IService));
BindingElementCollection elements = endpoint.Binding.CreateBindingElements();
elements.Find<TransportBindingElement>().MaxReceivedMessageSize = 5000;
endpoint.Binding = new CustomBinding(elements);
It's important to notice here that we aren't just altering the binding that is already present. The setting in this example is one that will be regenerated every time the binding creates a new set of binding elements. That means that we have to capture a snapshot of the binding, modify the snapshot, and then set that as a custom binding for the endpoint.