Question

I'm trying to expose an interface through NetNamedPipeBinding.

Here's what I do:

        try
        {
            //load the shedluer static constructor
            ServiceHost svh = new ServiceHost(typeof(MyClass));

            var netNamedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);

            var netNamedPipeLocation = "net.pipe://localhost/myservice/";
            svh.AddServiceEndpoint(typeof(IMyInterface), netNamedPipeBinding, netNamedPipeLocation);

            // Check to see if the service host already has a ServiceMetadataBehavior
            ServiceMetadataBehavior smb = svh.Description.Behaviors.Find<ServiceMetadataBehavior>();
            // If not, add one
            if (smb == null)
                smb = new ServiceMetadataBehavior();

            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            svh.Description.Behaviors.Add(smb);

            // Add MEX endpoint
            svh.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName,
                MetadataExchangeBindings.CreateMexNamedPipeBinding(),
                netNamedPipeLocation + "/mex"
                );

            svh.Open();

            Console.WriteLine("Service mounted at {0}", netNamedPipeLocation);
            Console.WriteLine("Press ctrl+c to exit");

            ManualResetEvent me=new ManualResetEvent(false);
            me.WaitOne();
            svh.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception");
            Console.WriteLine(e);
        }

The service starts ok, but when I create a new Visual Studio project and try to add a service reference at location net.pipe://localhost/myservice/, I get following error:

The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.pipe://localhost/myservice/'.
Metadata contains a reference that cannot be resolved: 'net.pipe://localhost/myservice/'.
If the service is defined in the current solution, try building the solution and adding the service reference again.

If I substitute NetNamedPipeBinding for TcpBinding, the code works ok, and service reference can be added.

What should I change, so I can add service references in Visual Studio?

Was it helpful?

Solution

With var netNamedPipeLocation = "net.pipe://localhost/myservice/"; netNamedPipeLocation + "/mex" winds up being net.pipe://localhost/myservice//mex. Your AddServiceEndPoint call should be

svh.AddServiceEndpoint(
            ServiceMetadataBehavior.MexContractName,
            MetadataExchangeBindings.CreateMexNamedPipeBinding(),
            netNamedPipeLocation + "mex"
            );

After removing the extra / I was able to connect to a local named pipe service hosted using your code without issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top