是否有可能使用WCF的发现揭露正在使用命名管道一个WCF终结?有可能、管道、发现、WCF

2023-09-04 00:22:18 作者:xx有一颗少女心

我使用发现,可能会从本地消耗的服务在同一台PC或遥控器上部署的应用程序。有没有什么办法来揭露命名管道通过WCF发现绑定?如果不是我想我可以协商的服务被发现后,以确定最合适的结合。

I have an application using discovery that may be deployed locally on the same PC or remote from the service it consumes. Is there any way to expose the named pipe binding via WCF discovery? If not I suppose I can negotiate after the service is discovered to determine the most suitable binding.

推荐答案

是的,这是可能做到这一点。但由于地址将被列为本地主机(唯一可能的地址net.pipe),你可能会需要一些测试,以验证该服务实际上是在同一台计算机作为搜索客户端上运行。

Yes, it's possible to do that. But since the address will be listed as "localhost" (the only possible address for net.pipe), you'll likely need some sort of test to verify that the service is actually running on the same machine as the discovery client.

public class StackOverflow_7068743
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text + " (via " + OperationContext.Current.IncomingMessageHeaders.To + ")";
        }
    }
    public static void Test()
    {
        string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service";
        string baseAddressPipe = "net.pipe://localhost/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddressHttp), new Uri(baseAddressPipe));
        host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
        host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.AddServiceEndpoint(typeof(ITest), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), "");
        host.Open();
        Console.WriteLine("Host opened");

        DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
        FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(ITest)));
        Console.WriteLine(findResponse.Endpoints.Count);

        EndpointAddress address = null;
        Binding binding = null;
        foreach (var endpoint in findResponse.Endpoints)
        {
            if (endpoint.Address.Uri.Scheme == Uri.UriSchemeHttp)
            {
                address = endpoint.Address;
                binding = new BasicHttpBinding();
            }
            else if (endpoint.Address.Uri.Scheme == Uri.UriSchemeNetPipe)
            {
                address = endpoint.Address;
                binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                break; // this is the preferred
            }

            Console.WriteLine(endpoint.Address);
        }

        if (binding == null)
        {
            Console.WriteLine("No known bindings");
        }
        else
        {
            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(binding, address);
            ITest proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Echo("Hello"));
            ((IClientChannel)proxy).Close();
            factory.Close();
        }

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}