NKn*_*rer 11 c# mono remoting nunit xamarin
我在玩.Net Remoting和Mono时发现了一个奇怪的问题.
当我在NUnit执行的代码中调用远程对象时抛出此异常:
System.Runtime.Remoting.RemotingException : Cannot create channel sink to connect to URL 9cd3eef9_6e1f_4e0e_a4fe_0d43b3b0d157/2d2f9b_6.rem. An appropriate channel has probably not been registered.
Stack trace:
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (System.Runtime.Remoting.Proxies.RealProxy rp, IMessage msg, System.Exception& exc, System.Object[]& out_args) [0x001e9] in /tmp/buildd/mono-4.0.1/mcs/class/corlib/System.Runtime.Remoting.Proxies/RealProxy.cs:249
Run Code Online (Sandbox Code Playgroud)
从Console应用程序执行完全相同的代码时,一切正常.
鉴于此接口:
public interface IEchoService {
string Echo(string message);
}
Run Code Online (Sandbox Code Playgroud)
和托管这样的实现的服务器:
var serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
var channel = new TcpServerChannel("RemotingTest", 4242, serverProvider);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloEchoService), "RemotingTest.rem", WellKnownObjectMode.Singleton);
Run Code Online (Sandbox Code Playgroud)
这将导致异常:
[Test]
public void SendEchoRequest()
{
var chan = new TcpClientChannel();
ChannelServices.RegisterChannel(chan, false);
string url = "tcp://localhost:4242/RemotingTest.rem";
var runner = (IEchoService)Activator.GetObject(typeof(IEchoService), url);
string echoMessage = runner.Echo("World");
Console.WriteLine(echoMessage);
Assert.That(echoMessage, Is.EqualTo("Hello World"));
}
Run Code Online (Sandbox Code Playgroud)
但是当从Console Main运行它时,它可以工作:
new Program().SendEchoRequest();
Run Code Online (Sandbox Code Playgroud)
我可以告诉NUnit和Console之间的主要区别是AppDomain.在NUnit中,代码在其中执行是自己的子AppDomain,Console在DefaultAppDomain中执行它.但是,在比较这两个AppDomain时,我没有发现任何差异.
在Windows计算机上运行NUnit时也会发生错误,但在Windows上托管服务器时却不会出现此错误.在这种情况下,Linux和Windows NUnit运行程序都可以成功执行测试.
在对Mono BCL进行一些调试后,我发现切换发生在System.Runtime.Serialization.Formatters.Binary/MessageFormatter.cs中.在第282行,读取MethodFlags.在NUnit中运行时,它们IncludesLogicalCallContext|PrimitiveArguments在控制台中ExcludeLogicalCallContext|PrimitiveArguments.
因此,第310行的if语句被输入objectReader.ReadObjectGraph执行的地方,最终导致System.Runtime.Remoting/RemotingServices.cs第695行中的异常.
我创建了一个下载示例解决方案来重现问题:https:
//mega.co.nz/#!xYQkwBba!g-jOex-EXSP6e3JYgh-FuWi7VhXjF4e6i494lO4J5po
只需运行服务器mono RemotingTest.Server.exe,然后使用MonoDevelop运行客户端测试.
我的测试环境是Ubuntu 15.04 x64,Mono 4.0.1来自官方Mono存储库.
| 归档时间: |
|
| 查看次数: |
1199 次 |
| 最近记录: |