Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

c# - Is it possible to use WCF discovery to expose a WCF endpoint that is using named pipes?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...