WCF: how to restrict using some protocols?
Question
WCF (winodws service hosting) service uses set of protocols and bindings: http, https, net.tcp, net.pipe. It uses config file settings.
I want to build demo version of the service. This demo will use only net.pipe protocol. How I can restrict service to use only this one? I can do changes in code , but how and where?
Solution
ServiceHost
owns collection of ChannelDispatcher
s in ChannelDispatchers
property. You can use ChannelDispatcher.BindingName
to figure out name of binding used in your service.
ServiceHost host = new ServiceHost(typeof(SomeService), baseAddress))
//configure service endpoints here
host.Open();
#if DEMO_MODE
foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
{
//binding name includes namespace. Example - http://tempuri.org/:NetNamedPipeBinding
var bindingName = dispatcher.BindingName;
if (!(bindingName.EndsWith("NetNamedPipeBinding") || bindingName.EndsWith("MetadataExchangeHttpBinding")))
throw new ApplicationException("Only netNamedPipeBinding is supported in demo mode");
}
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow