向NetNamedPipeBinding添加mex端点

本文关键字:端点 mex 添加 NetNamedPipeBinding | 更新日期: 2023-09-27 18:18:01

我试图通过NetNamedPipeBinding公开一个接口。

我是这样做的:

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

服务启动正常,但当我创建一个新的Visual Studio项目并尝试在位置net.pipe://localhost/myservice/添加服务引用时,我得到以下错误:

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.

如果我用NetNamedPipeBinding代替TcpBinding,代码可以正常工作,并且可以添加服务引用。

我应该改变什么,以便我可以在Visual Studio中添加服务引用?

向NetNamedPipeBinding添加mex端点

随着var netNamedPipeLocation = "net.pipe://localhost/myservice/";, netNamedPipeLocation + "/mex"最终成为net.pipe://localhost/myservice//mex。你的AddServiceEndPoint呼叫应该是

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

在删除了额外的/之后,我可以连接到使用你的代码托管的本地命名管道服务。