テスト駆動型の方法で C# の下で Remoting を使用することから始めたかったのですが、行き詰まってしまいました。
このトピックで見つけたのは、Marc Clifton によるこの記事ですが、彼はコンソールから手動で起動してサーバーを実行しているようです。
テスト フィクスチャでサーバーを起動しようとしました (つまり、サービング クラスを登録します)。おそらくインターフェイスの使い方も間違っていると思いますが、それは後で説明します。
チャンネルが既に登録されているという例外(ドイツ語のメッセージで申し訳ありません)が常に発生します。System.Runtime.Remoting.RemotingException : Der Channel tcp wurde bereits registriert.
テスト メソッドの ChannelServices.RegisterChannell() 行をコメント アウトした後、Activator.GetObject() の呼び出しに対して発生します。
StartServer() をスレッドに入れようとしましたが、それも役に立ちませんでした。新しい AppDomain を作成することが可能な方法であることがわかりましたが、まだ試していません。
私のアプローチが本質的に間違っているかどうか教えてもらえますか? どうすれば修正できますか?
using System;
using NUnit.Framework;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Bla.Tests.Remote
{
[TestFixture]
public class VerySimpleProxyTest
{
int port = 8082;
string proxyUri = "MyRemoteProxy";
string host = "localhost";
IChannel channel;
[SetUp]
public void SetUp()
{
StartServer();
}
[TearDown]
public void TearDown()
{
StopServer();
}
[Test]
public void UseRemoteService()
{
//IChannel clientChannel = new TcpClientChannel();
//ChannelServices.RegisterChannel(clientChannel, false);
string uri = String.Format("tcp://{0}:{1}/{2}", host, port, proxyUri);
IMyTestService remoteService = (IMyTestService)Activator.GetObject(typeof(IMyTestService), uri);
Assert.IsTrue(remoteService.Ping());
//ChannelServices.UnregisterChannel(clientChannel);
}
private void StartServer()
{
channel = new TcpServerChannel(port);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyTestService), proxyUri, WellKnownObjectMode.Singleton);
}
private void StopServer()
{
ChannelServices.UnregisterChannel(channel);
}
}
public interface IMyTestService
{
bool Ping();
}
public class MyTestService : MarshalByRefObject, IMyTestService
{
public bool Ping()
{
return true;
}
}
}