Posts Tagged ‘WCF’
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);
}
}
WCF Setup Problems
WCF has to be one of the most frustrating technologies I have ever used. The problems I encountered tonight were in the setup. Like most things development, the trick is in the setup. Unfortunately, I spend a lot of time tweaking the settings on my new development machine install, but I never remember or even note down the tweaks I made to get things to work. So when I rebuild my machine, I am back to square one.
The WCF configuration has too many things that not only look the same, but the declarative nature – while flexible – in most cases is just a pain in the ass and I found it easier to simply hard code the values as in my experience, they rarely change.
Anyway, setting up my WCF IIS host on a new development machine proved to be more involved than I initially credited it would be.
When setting up the IIS host for a WCF library you will need to create an SVC file. But, this file extension may not be recognised by IIS – making things ‘tricky’. So here are some things to look out for in order:
- Try installing the ASP components from the Windows Components in Programs & Services.
- Try ServiceModelReg -ia to install all the WCF components onto your machine.
- Try asp net-regiis -i to install the handler configurations to IIS.
- Sometimes you get a problem where you do not have permissions to run the web application from the current – normally – default application pool. So try setting the user account to LocalSystem in IIS for the corresponding application pool.