0

WCF(winodws 服务托管)服务使用一组协议和绑定:http、https、net.tcp、net.pipe。它使用配置文件设置。

我想构建服务的演示版本。此演示将仅使用 net.pipe 协议。我怎样才能限制服务只使用这个?我可以更改代码,但是如何以及在哪里?

4

1 回答 1

1

ServiceHost在财产中拥有ChannelDispatchers 的集合。ChannelDispatchers您可以使用ChannelDispatcher.BindingName来确定服务中使用的绑定名称。

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
于 2012-08-21T03:31:40.587 回答