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

Jör*_*ers 2 wcf ip-address wcf-binding wcf-client

如何在调用WCF服务时定义WCF客户端使用的LocalEndPoint(如果客户端计算机有多个IP地址)?

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

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

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

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

提前感谢任何有用的建议!!

这似乎是一个类似的问题,只使用TcpClient/Sockets而不是WCF: 在C#中指定用于TCPClient/Socket的传出IP地址

另一个,这次是关于SoapClient: 在发送传出请求之前将新的SoapClient绑定到特定的IP地址

小智 5

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

下面是一个返回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);
    }
Run Code Online (Sandbox Code Playgroud)

此外,当我在测试服务器上时,我不得不使用代理来获取服务.(我在有权访问该服务的机器上使用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);
    }
Run Code Online (Sandbox Code Playgroud)