WCF:Net.TCP多个绑定(bind),相同端口,不同IP地址

标签 wcf wcf-binding net.tcp

我遇到了一个问题。我是 WCF 的新手,因此我们将不胜感激任何帮助。

这是我的代码:

public static void StartHosts()
    {
        try
        {
            // Create a new host
            ServiceHost host = new ServiceHost(typeof(ServerTasks));

            List<IPAddress> ips = new List<IPAddress>(Dns.GetHostAddresses(Dns.GetHostName()));
            if (IPAddress.Loopback != null)
                ips.Add(IPAddress.Loopback);

            ips.RemoveAll(i => i.AddressFamily != AddressFamily.InterNetwork);

            foreach (var ip in ips)
            {
                string uri = string.Empty;

                // Formulate the uri for this host
                uri = string.Format(
                    "net.tcp://{0}:{1}/ServerTasks",
                    ip.ToString(),
                    ServerSettings.Instance.TCPListeningPort
                );


                // Add the endpoint binding
                host.AddServiceEndpoint(
                    typeof(ServerTasks),
                    new NetTcpBinding(SecurityMode.Transport) { TransferMode = TransferMode.Streamed },
                    uri
                );

            }



            // Add the meta data publishing
            var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
                smb = new ServiceMetadataBehavior();

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

            host.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName,
                MetadataExchangeBindings.CreateMexTcpBinding(),
                "net.tcp://localhost/ServerTasks/mex"
            );

            // Run the host
            host.Open();
        }
        catch (Exception exc)
        {
            DebugLogger.WriteException(exc);
        }
    }

行上抛出异常:'host.Open();'

异常(exception)情况是:

System.InvalidOperationException URI“net.tcp://192.168.1.45:4329/ServerTasks”的注册已存在。

我想做的是绑定(bind)到计算机上的所有网络地址,以便客户端应用程序可以从他们在其上看到的任何网络访问该服务。当我运行此代码时,它会找到并尝试为大约 5 个不同的 IP 设置绑定(bind),其中包括 127.0.0.1。

192.168.1.45 是它尝试绑定(bind)的第二个 IP。在它抛出异常时,我可以看到(使用 netstat)该程序已绑定(bind)到端口 4329 上列表中的第一个 IP。异常中提到的地址上没有任何绑定(bind)到端口 4329 的内容。

抱歉,没有太多细节,我想发表一篇简洁的文章。如果有人需要更多信息,我很乐意提供。

注意:我尝试将在 foreach 循环内创建的 NetTcpBinding 设置 PortSharingEnabled 为 true,但仍然遇到相同的错误。

任何帮助或建议将不胜感激!

谢谢

最佳答案

感谢科拉扎提供的信息!

我已经想出了如何实现这一点。我的做法完全错误。

我的最终目标是让该服务监听计算机上可用的每个 IP 地址。尝试单独绑定(bind)到每个地址是错误的方法。

相反,我只需将服务绑定(bind)到计算机的主机名(而不是“localhost”),WCF 就会自动监听所有适配器。

这是更正后的代码:

public static void StartHosts()
    {
        try
        {
            // Formulate the uri for this host
            string uri = string.Format(
                "net.tcp://{0}:{1}/ServerTasks",
                Dns.GetHostName(),
                ServerSettings.Instance.TCPListeningPort
            );

            // Create a new host
            ServiceHost host = new ServiceHost(typeof(ServerTasks), new Uri(uri));

            // Add the endpoint binding
            host.AddServiceEndpoint(
                typeof(ServerTasks),
                new NetTcpBinding(SecurityMode.Transport) 
                        { 
                            TransferMode = TransferMode.Streamed
                        },
                uri
            );

            // Add the meta data publishing
            var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
                smb = new ServiceMetadataBehavior();

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

            host.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName,
                MetadataExchangeBindings.CreateMexTcpBinding(),
                "net.tcp://localhost/ServerTasks/mex"
            );

            // Run the host
            host.Open();
        }
        catch (Exception exc)
        {
            DebugLogger.WriteException(exc);
        }
    }

关于WCF:Net.TCP多个绑定(bind),相同端口,不同IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/703051/

相关文章:

.net - 错误 400(错误请求): maxReceivedMessageSize not taken into account for my WCF service

WCF 这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol)

wcf - 我是否需要 svc 文件来为非 HTTP 服务设置 CaSTLe Wcf 设施

wcf - WCF 服务最佳实践 - Windows Azure

javascript - 如果列没有数据, Bootstrap 表删除行

wcf - 在 IIS 中托管时,使用 HTTPS 进行 Webhttpbinding 和使用 UserNamepPasswordValidator 进行身份验证

wcf - 启用传输安全性后,Mono WCF 客户端无法与 .NET WCF 服务器通信

wcf - WCF 服务中的 IncomingWebRequestContext.UriTemplateMatch null

.net - net.pipe、net.tcp 与 http 绑定(bind)

wcf - 在两个 tcp 端口上使用 net.tcp 绑定(bind)在 IIS 7 (WAS) 中托管 WCF 服务