WCF UDP发现仅适用于BasicHttpBinding,不适用于NetTcpBinding

时间:2018-10-04 08:49:03

标签: c# wcf wcf-binding

我有一个WCF服务,该服务已经宣布了两个服务端点。一种用于net.tcp,一种用于http。

  

net.tcp:// localhost:11110 / MyService

     

http://localhost:11111/MyService

        // Make service discoverable 
        var discoveryBehavior = new ServiceDiscoveryBehavior();
        var announceEndpointAddress = ConfigurationManager.AppSettings["AnnouncementEndpoint"];

        var netTcpBinding = new NetTcpBinding(SecurityMode.None)
        {
            ReceiveTimeout = TimeSpan.MaxValue,
            SendTimeout =  TimeSpan.MaxValue,
            OpenTimeout =TimeSpan.MaxValue,
            CloseTimeout =  TimeSpan.MaxValue
        };

        discoveryBehavior.AnnouncementEndpoints.Add(
            new AnnouncementEndpoint(netTcpBinding,
            new EndpointAddress(announceEndpointAddress)
        ));
        _serviceHost.Description.Behaviors.Add(discoveryBehavior);
        _serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

在客户端,我正在使用WCF发现。

class Program
{
    static void Main(string[] args)
    {
        var optionalBindings = new Binding[]
        {
            new NetTcpBinding(),
            new BasicHttpBinding(),
            new NetNamedPipeBinding()
        };

        var executed = false;

        foreach (var binding in optionalBindings)
        {
            var contract = ContractDescription.GetContract(typeof(IMyService));
            var endpoint = new DynamicEndpoint(contract, binding);

            var factory = new ChannelFactory<IMyService>(endpoint);
            var proxy = factory.CreateChannel();

            try
            {
                var result = proxy.GetData(123456);

                ((ICommunicationObject) proxy).Close();

                executed = true;
                break;
            }
            catch (EndpointNotFoundException)
            {
                // Ignored
            }
        }
    }
}

让我感到困惑的是,它仅适用于BasicHttpBinding,而不适用于NetTcpBinding。因此,如果从optionalBindings列表中选择BasicHttpBinding,尽管有net.tcp端点,但我无法调用该服务。

有人可以解释原因吗?

0 个答案:

没有答案