DOS WCF host and client
It’s no secret that I have no love for the WCF declarative configuration and I find testing through an IIS host a whole faff. This is why I use a DOS host and client to easily test the transportation of data as my first port of call in testing.
WCF DOS Host
private static void RunHost()
{
var host = new ServiceHost(typeof(TestWcfService));
var httpBinding = new BasicHttpBinding
{
MaxBufferPoolSize = 0,
MaxReceivedMessageSize = Int32.MaxValue,
MaxBufferSize = Int32.MaxValue,
TransferMode = TransferMode.Streamed
};
var wsHttpBinding = new WSHttpBinding
{
MaxBufferPoolSize = 0,
MaxReceivedMessageSize = Int32.MaxValue,
ReliableSession = {Enabled = true, Ordered = true}
};
var netTcpBinding = new NetTcpBinding
{
MaxBufferPoolSize = 0,
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
ReliableSession = {Enabled = true, Ordered = true}
};
var netNamedPipe = new NetNamedPipeBinding
{
MaxBufferPoolSize = 0,
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue
};
host.AddServiceEndpoint(typeof(ITestService), httpBinding, “http://localhost/basic”);
host.AddServiceEndpoint(typeof(ITestService), wsHttpBinding, “http://localhost/ws”);
host.AddServiceEndpoint(typeof(ITestService), netTcpBinding, “net.tcp://localhost/tcp”);
host.AddServiceEndpoint(typeof (ITestService), netNamedPipe, “net.pipe://localhost/pipe”);
var operation = host.Description.Endpoints[0].Contract.Operations.Find(“TestCollectionParameter”);
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = Int32.MaxValue;
try
{
host.Open();
PrintConnectionInformation(host);
Console.WriteLine(“Listening….”);
Console.ReadLine();
Console.WriteLine(“Closing channels…”);
host.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
host.Abort();
throw;
}
}
private static void PrintConnectionInformation(ServiceHostBase host)
{
host.Description.Endpoints.ToList().ForEach(ep => Console.WriteLine(String.Format(“{0} running at {1}”, ep.Name, ep.ListenUri)));
}
WCF DOS Client
public class CustomChannelFactory<T> : ChannelFactory<T>
{
public CustomChannelFactory(Binding binding) : base(binding) { }
public CustomChannelFactory(ServiceEndpoint endpoint) : base(endpoint) { }
public CustomChannelFactory(string endpointConfigurationName) : base(endpointConfigurationName) { }
public CustomChannelFactory(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
public CustomChannelFactory(Binding binding, string remoteAddress) : base(binding, remoteAddress) { }
public CustomChannelFactory(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
protected override void OnOpening()
{
foreach (var behavior in
Endpoint.Contract.Operations.Select(operation => operation.Behaviors.Find<DataContractSerializerOperationBehavior>()).Where(behavior => behavior != null))
{
behavior.MaxItemsInObjectGraph = int.MaxValue;
}
base.OnOpening();
}
}
[ServiceContract]
public interface ITestService
{
[OperationContract]
string TestStringReturn();
[OperationContract]
string TestCollectionParameter(List<string> data);
}
private static void TestConnectBasicHttpBinding()
{
var binding = new BasicHttpBinding
{
MaxBufferSize = Int32.MaxValue,
MaxReceivedMessageSize = Int32.MaxValue,
MaxBufferPoolSize = Int32.MaxValue,
TransferMode = TransferMode.Streamed,
SendTimeout = new TimeSpan(0, 0, 10, 0),
ReceiveTimeout = new TimeSpan(0, 0, 10, 0)
};
Console.WriteLine(“Connecting through basic HTTP channel.”);
var factory = new CustomChannelFactory<ITestService>(binding, “http://localhost/basic”);
var channel = factory.CreateChannel();
try
{
var x = channel.TestStringReturn();
Console.WriteLine(“Returned Value: {0}”, x);
}
catch (Exception ex)
{
if (channel != null)
((IClientChannel)channel).Abort();
Console.WriteLine(ex.Message);
}
}