4

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?

Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224

1 Answers1

3

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.

Preston Guillot
  • 6,493
  • 3
  • 30
  • 40
  • why are you using "MetadataExchangeBindings.CreateMexNamedPipeBinding()" instead of "NetNamedPipeBinding()"? What is the difference? – progLearner Feb 19 '20 at 13:37
  • @progLearner I am using what the OP used. That said, they're adding a mex endpoint, so the helper factory is appropriate. – Preston Guillot Mar 08 '20 at 12:29