在WCF连接中更改ChannelFactory的IPEndpoint

时间:2017-09-21 10:41:37

标签: c# asp.net .net wcf

是否可以使用ServicePoint将客户端的传出IP地址更改为WCF服务?

var localIpAddress = IPAddress.Parse("192.168.1.113");
ServicePoint servicePoint = ServicePointManager.FindServicePoint(new Uri(oDataSource.wcfURL));
servicePoint.BindIPEndPointDelegate =
                (sp, rm, retryCount) =>
                {
                    //Console.WriteLine(retryCount);
                    return new IPEndPoint(localIpAddress, 80);
                };

DuplexChannelFactory<IBSPubSubService> oChannelFactory = new DuplexChannelFactory<IBSPubSubService>(new InstanceContext(this), oBinding, oEndpoint);
oPubSubClient = oChannelFactory.CreateChannel();
oPubSubClient.Faulted += new EventHandler(WcfFaultedListener);
oPubSubClient.Opened += new EventHandler(WcfOpenListener);

我尝试使用上面的代码但不起作用。看起来像BindIPEndpointDelegate在调用oChannelFactory.CreateChannel()后进入无限循环。

1 个答案:

答案 0 :(得分:0)

尝试绑定失败,直到 int.MaxValue

  

如果您无法选择本地端点,只需返回null并自动为您选择端点。

这是a link