远程呼叫永远不会返回。可能是远程冲突?

时间:2016-03-11 23:03:29

标签: c# .net-remoting

我正在尝试为第三方应用建立.Net远程调用。这是我为该连接提供的示例代码(删除了专有名称):

IDictionary props = new ListDictionary();
props["port"] = 0; // have the Remoting system pick a unique listening port (required for callbacks)
props["name"] = string.Empty; // have the Remoting system pick a unique name
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
_channel = new TcpChannel(props, new BinaryClientFormatterSinkProvider(), serverProv);
ChannelServices.RegisterChannel(_channel, true);

IThirdparty _thirdparty = (IThirdparty)Activator.GetObject(typeof(IThirdparty), "tcp://localhost:9090/Thirdparty.AppIntegration");

//Example API call
_thirdparty.Minimized = !_thirdparty.Minimized;

当正常调用此代码时,它会挂起_thirdparty.Minimized并在诊断窗口中输出SocketException并显示以下消息:

No connection could be made because the target machine actively refused it

只有在我关闭Thirdparty应用程序时才会返回此呼叫。

我检查了netstat -bano,在端口9090上运行的唯一应用就是我要连接的应用。

所以我将调用移到了应用程序中Main()函数的前几行,它运行得很好。问题是,这不是它应该在的地方。

我的应用程序包含许多其他远程调用到不同服务器(不在端口9090上)以及WCF服务。我的猜测是其中一件事正在干扰。

关于我如何弄清楚为什么这个远程调用永远不会返回的任何想法?

更新 我已经确定SocketException可能是一个红色的鲱鱼,因为这些异常是在第三方测试应用程序中运行时创建的。此外,它看起来像挂起的原因是因为它正在等待一个永远不会得到任何数据的Socket.Read()。

1 个答案:

答案 0 :(得分:0)

事实证明,在.NET远程处理中,每个AppDomain只能有一个TCPClientChannel。 Activator.GetObject()使用已注册的第一个通道。

这是阻塞的原因是因为我已经在我的AppDomain中设置了TCPClientChannel。注册时,此通道的安全设置为false。即。

ChannelServices.RegisterChannel(_channel, false);

我试图与之交谈的服务启用了安全性,因此远程调用会挂起,试图听取永远不会发生的响应。

解决方案是将我的集成加载到新的AppDomain中,以便我可以以不同方式配置TCPChannel。