首页 文章

指定要与WCF客户端一起使用的传出IP地址

提问于
浏览
2

How to define the LocalEndPoint to use by a WCF client when calling a WCF service (if the client machine has multiple IP addresses) ?

我有一台位于DMZ中的机器有两个IP地址,外部IP地址可以通过防火墙通过我们位于外部服务提供商的网络服务器的VPN连接到达 . 在此计算机上运行基于WCF和Unity的自定义应用程序服务器,该服务器应充当代理或应用程序级网关(ALG) . 它应接受来自Web服务器的服务调用,并使用wcf客户端工厂重新生成服务调用,将它们转发到LAN中的真实应用服务器 .

当使用wcf客户端工厂在此代理上重新创建服务调用时,wcf客户端应使用此计算机的第二个内部IP地址,因为只允许来自此内部IP地址的消息通过防火墙才能到达局域网中的应用服务器 . 不幸的是,我们的wcf客户端代理始终选择使用第一个“外部”IP地址创建传出消息 . 我正在寻找一种方法来明确设置wcf客户端代理使用的IP地址 .

我只能找到一个允许定义LocalEndPoint或ClientBaseAddress的WCF绑定元素:CompositeDuplexBindingElement . 据我所知,这个属性是告诉服务器在哪里发送asynch回复消息,所以它与我正在寻找的设置不同 .

任何想法我能做些什么才能找到可行的解决方案?

Thanks in advance for any helpful advice!!

这似乎是一个类似的问题,只使用TcpClient / Sockets而不是WCF:Specify the outgoing IP address to use with TCPClient / Socket in C#

还有一个,这次关于SoapClient:Binding a new SoapClient to a specific IP address before sending outgoing request

3 回答

  • 0

    很难自己找到这个答案,所以我会分享我的解决方案以防其他人来到这里 .

    下面是一个返回wcf soapservice的方法 . 代码将尝试使用特定的ip(如果可用) . 我做了这个检查,让它也在测试机上工作 .

    public static proposalSoapClient getService()
        {
            string serviceurl = "http://somesite.dk/proposal.asmx";
            var localIpAddress = IPAddress.Parse("123.123.123.123");
            var hasLocalAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(ip => ip.AddressFamily == AddressFamily.InterNetwork && ip.Equals(localIpAddress));
    
            var binding = new System.ServiceModel.BasicHttpBinding("proposalSoap"); //name of binding in web.config
            var endpoint = new EndpointAddress(serviceurl);
            ServicePoint servicePoint = ServicePointManager.FindServicePoint(new Uri(serviceurl));
            servicePoint.BindIPEndPointDelegate = (sp, rm, retryCount) => { return new IPEndPoint(hasLocalAddress ? localIpAddress : IPAddress.Any, 0); };
            return new proposalSoapClient(binding, endpoint);
        }
    

    此外,当我在测试服务器上时,我不得不使用代理来获取服务 . (我在有权访问该服务的机器上使用fiddler作为代理) . 下面是相同的代码,但在测试服务器上添加了代理部分 .

    public static proposalSoapClient getService()
        {
            string serviceurl = "http://somesite.dk/proposal.asmx";
            var localIpAddress = IPAddress.Parse("123.123.123.123");
            var hasLocalAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Any(ip => ip.AddressFamily == AddressFamily.InterNetwork && ip.Equals(localIpAddress));
    
            var binding = new System.ServiceModel.BasicHttpBinding("proposalSoap"); //name of binding in web.config
            var endpoint = new EndpointAddress(serviceurl);
            ServicePoint servicePoint = ServicePointManager.FindServicePoint(new Uri(serviceurl));
    
        #if DEBUG
            Uri proxyUri = new Uri("http://someothersite.dk:8888");
            binding.ProxyAddress = proxyUri;
            binding.BypassProxyOnLocal = false;
            binding.UseDefaultWebProxy = false;
            servicePoint = ServicePointManager.FindServicePoint(serviceurl, new WebProxy(proxyUri, false));
        #endif
    
            servicePoint.BindIPEndPointDelegate = (sp, rm, retryCount) => { return new IPEndPoint(hasLocalAddress ? localIpAddress : IPAddress.Any, 0); };
            return new proposalSoapClient(binding, endpoint);
        }
    
  • 1

    这是一个较旧的主题,但我建议有人找到这个,因为它引导我找到一个稍微不同的解决方案 . 我们需要在同一台服务器上的不同进程上运行两个进程 .

    问题是每个默认选择绑定到NIC的主地址 . 我们找不到让应用程序绑定到NIC上的辅助地址的方法 .

    解决方法是向VM添加第二个NIC,将辅助IP作为第二个NIC的主IP移动到第二个NIC . 在Windows中,您不能拥有两个默认网关(即使它们指向相同的地址) .

    因此,在第二个NIC上,我们仅分配了IP和子网掩码 . 为了让流量路由出正确的接口,我们添加了一个路由语句,格式为:

    route add 4.3.2.1 mask 255.255.255.255 1.2.3.4 IF 0x10002
    

    我们需要进程与之交谈的主机是 4.3.2.1 ,本地默认网关是 1.2.3.4 ,第二个NIC在路由表中显示为接口号 0x10002 (来自路由打印) .

    我希望这有帮助 . 因为这个,我还有一些白发 .

  • 5

    听起来像路由表在服务器上有点混乱,你想要做什么 . 而不是试图在应用程序中纠正它,调整机器上的接口度量和/或路由表,以优先选择内部接口用于发往该网络的流量 . Here是旧的参考,但应该或多或少有效 .

    我不认为.NET套接字会允许你明确地选择一个源接口 - 最接近的东西就是DontRoute SocketOption,但即使在那里,你可能不得不滚动自定义绑定和传输以使该选项可用 .

相关问题