Custom ChannelFactory Creation
Just the other day, Derik Whitaker ran into some issues setting up his ChannelFactory to handle large object graphs being returned to his clients (post is here). After some back and forth through email, we came up with a solution. Instead of use the default ChannelFactory
The trick is to override the ChannelFactory
public class DerikChannelFactory
public DerikChannelFactory(ServiceEndpoint endpoint) : base(endpoint) { }
public DerikChannelFactory(string endpointConfigurationName) : base(endpointConfigurationName) { }
public DerikChannelFactory(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
public DerikChannelFactory(Binding binding, string remoteAddress) : base(binding, remoteAddress) { }
public DerikChannelFactory(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
protected override void OnOpening() { foreach (var operation in Endpoint.Contract.Operations) { var behavior = operation.Behaviors. Find<DataContractSerializerOperationBehavior>(); if (behavior != null) { behavior.MaxItemsInObjectGraph = int.MaxValue; } } base.OnOpening(); } }
The OnOpening override is also a good place to inject behaviors or other items if you want to make sure that all ChannelFactory instances have the same setup without resorting to configuration or code for each instance.