[英]Testdriven Remoting in C# - Do I need a separate server AppDomain?
我想開始在測試驅動的方式下在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;
}
}
}
我真的沒有解決你的問題,但我的建議是,不要以這種方式編寫單元測試。 看這篇文章 。 你真的想在這里測試什么代碼。 我很確定微軟已經對.net附帶的遠程處理功能進行了大量測試。 實現Service接口的類可以通過新增實現在進程中進行單元測試。 如果.net框架沒有使用靜態注冊位,那么注冊您的服務接口的代碼將是可測試的,但是唉。 您可以嘗試將IChannel模擬傳遞給ChannelServices.RegisterChannel
並以某種方式驗證您的注冊碼,但在我的選擇中,這將浪費時間。
我想說的是,測試應該是達到目的的手段,而不是目標本身。
我找到了一個很好的方法來完成我想做的事情,只使用WCF而不是Remoting。
我在不到5分鍾的時間內將Yair Cohen撰寫的文章中的源代碼移植到NUnit,它開箱即用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.