如何在发出请求之前将 WCF Http 客户端绑定到特定的出站 IP 地址

发布于 2024-09-16 00:13:02 字数 567 浏览 0 评论 0原文

我希望我的请求通过特定的 IP 地址发出。有没有办法在 WCF 中做到这一点?为什么我需要这个的解释有点冗长,所以我不想深入讨论。

这是示例代码

string ipAddress = "192.168.0.32";
IService service;
ChannelFactory<IOmlService> factory = new ChannelFactory<IService>(new BasicHttpBinding(), new EndpointAddress("http://" + IPAddress + ":6996/IService"));
service = factory.CreateChannel();
service.Test();

这是一个示例场景,可以准确解释我正在寻找的内容。假设我的机器上有两个 IP(192.168.0.30 和 192.168.0.31)。两者都可以打到192.168.0.32。如果我现在运行此代码,它将从我的任何 IP(.30 或 .31)访问 IP(.32)。我怎样才能强制它通过我的特定IP(比如0.30)。有没有办法使用 WCF 来做到这一点?

I want my request to go out through a specific IP Addresses. Is there a way to do that in WCF. The explanation of why I need this is a little long winded so i'd rather not get into that.

Here is sample code

string ipAddress = "192.168.0.32";
IService service;
ChannelFactory<IOmlService> factory = new ChannelFactory<IService>(new BasicHttpBinding(), new EndpointAddress("http://" + IPAddress + ":6996/IService"));
service = factory.CreateChannel();
service.Test();

Here is an example scenario to explain exactly what i'm looking for. Let's say I have two IPs on my machine (192.168.0.30 and 192.168.0.31). Both of them can hit 192.168.0.32. If i run this code now, it will hit the IP (.32) from any of my IPs (.30 or .31). How can i force it to go through a specific IP of mine (say .30). Is there any way to do that using WCF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫多说 2024-09-23 00:13:02

问题的答案是做不到。这是来自 Microsoft MVP 的回答


那么您想让客户端计算机主动选择其中一个网络适配器接口(安装在其上)来发送 WCF 请求吗?恐怕这超出了 WCF 的控制范围,因为 WCF 只关注以下地址:

** 当作为主机时,我们可以选择绑定到特定的主机名/地址来侦听客户端请求
** 当作为客户端时,我们可以选择要发送请求的目标地址/主机名。


The answer to the question is that it cannot be done. Here is the answer from a Microsoft MVP


So you want to let the client-side machine proactively select one of the network adpater interface(installed on it) to send out the WCF requests? I'm afraid this is out of WCF's control since WCF only focus on the following addresses:

** when behave as a host, we can choose to bind to a specific hostname/address to listen for client requests
** when behave as a client, we can choose the destination address/hostname to send request to.


清引 2024-09-23 00:13:02

您是否正在尝试在服务器上制作类似于 IP-Sec 的东西,以便它只接受来自特定 IP 地址的请求?

在这种情况下,您需要实现 IEndpointBehavior 和 IDispatchMessageInspector 并且:

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        RemoteEndpointMessageProperty remoteAddress =
            (OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as
             RemoteEndpointMessageProperty);

        // validate ip here
        return null;
    }

Are you trying to make something similar to IP-Sec on the Server so it only accepts request from specific IP addresses?

In this case, you need to implement IEndpointBehavior and IDispatchMessageInspector and:

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        RemoteEndpointMessageProperty remoteAddress =
            (OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as
             RemoteEndpointMessageProperty);

        // validate ip here
        return null;
    }
守护在此方 2024-09-23 00:13:02

在我看来,您的问题应该通过在路由表中设置额外的死记硬背来解决。尝试从使用管理权限启动的命令提示符中执行以下操作:

route add 192.168.0.32 mask 255.255.255.255 192.168.175.30

如果要保存路由,请另外添加 -p 开关。

It seems to me that your problem should be solved by setting of an additional rote in the routing table. Try do following from the Command Prompt started with administrative rights:

route add 192.168.0.32 mask 255.255.255.255 192.168.175.30

If you want to save the route add -p switch additionally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文