In a recent project I wanted to simplify the creation and configuration of a WCF proxy client and enforce programmatic configuration so that the client could only be used in a specific configuration. Here’s the result of that effort. Note that there is a static Create method and the standard ClientBase constructors have been marked as internal to prevent creation of the client in any other way.
public class ControlServiceClient : ClientBase<IControlService>, IControlService
{
public static ControlServiceClient Create()
{
string controlServiceAddress = ConfigurationManager.AppSettings["controlServiceAddress"];
NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.Transport, false);
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
tcpBinding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
//set other binding attributes
tcpBinding.CloseTimeout = new TimeSpan(0, 1, 30); //default is 1 minute.
tcpBinding.MaxBufferPoolSize = 1048576; //1MB default is 65,536 bytes
//not allowed by partially trusted
//tcpBinding.MaxBufferSize = 262144; //256KB default is 65,536 bytes
tcpBinding.MaxConnections = 10; //default is 10:
tcpBinding.MaxReceivedMessageSize = 4194304; //4MB The default is 65,536 bytes
tcpBinding.OpenTimeout = new TimeSpan(0, 1, 30); //The default value is 1 minute
tcpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0); //The default value is 10 minute
tcpBinding.SendTimeout = new TimeSpan(0, 1, 30); //The default value is 1 minute
EndpointAddress endpointAddress =
new EndpointAddress(string.Format("net.tcp://{0}", controlServiceAddress));
ControlServiceClient client = new ControlServiceClient(tcpBinding, endpointAddress);
return client;
}
internal ControlServiceClient() { }
internal ControlServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{ }
internal ControlServiceClient(Binding binding, EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{ }
internal ControlServiceClient(InstanceContext callbackInstance) :
base(callbackInstance)
{ }
public string GetData(int value)
{
return base.Channel.GetData(value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
return base.Channel.GetDataUsingDataContract(composite);
}
}