C#中的测试驱动远程处理-我需要单独的服务器AppDomain吗?

C#中的测试驱动远程处理-我需要单独的服务器AppDomain吗?,c#,unit-testing,remoting,C#,Unit Testing,Remoting,我想以测试驱动的方式开始在C#下使用远程处理,但我被卡住了 我在这个主题中发现了一件事,但他似乎通过从控制台手动启动服务器来运行服务器 我尝试在测试夹具中启动服务器(即注册服务类)。 我可能也有使用错误的接口,但这将在以后 我总是得到一个例外(很抱歉,德国消息),该频道已注册。 System.Runtime.Remoting.RemotingException:Der Channel tcp wurde bereits registriert 在注释掉测试方法中的ChannelServices.

我想以测试驱动的方式开始在C#下使用远程处理,但我被卡住了

我在这个主题中发现了一件事,但他似乎通过从控制台手动启动服务器来运行服务器

我尝试在测试夹具中启动服务器(即注册服务类)。 我可能也有使用错误的接口,但这将在以后

我总是得到一个例外(很抱歉,德国消息),该频道已注册。 System.Runtime.Remoting.RemotingException:Der Channel tcp wurde bereits registriert

在注释掉测试方法中的ChannelServices.registerchannel()行后,调用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;
        }
    }
}

我找到了一个很好的方法来做我想做的事情,只是使用WCF而不是远程处理


我在不到5分钟的时间内将提供的源代码移植到NUnit,它可以开箱即用。

我没有解决您的问题的方法,但我的建议是,不要以这种方式编写单元测试。看这个。你真正想在这里测试什么代码。我敢肯定微软已经对.net附带的远程处理功能进行了大量测试。实现服务接口的类可以通过更新实现在进程中进行单元测试。如果.net framework没有对注册位使用静态,那么注册服务接口的代码本来是可以测试的,但遗憾的是。您可以尝试将IChannel模拟传递给
ChannelServices.RegisterChannel
,并以某种方式验证您的注册码,但我认为这是浪费时间


我想说的是,测试应该是达到目的的一种手段,而不是目标本身。

测试驱动不仅仅意味着单元测试,它可以包括集成测试和事务的端到端测试。可能TDD的人会哭,但除了单元测试之外,我倾向于在单元测试工具中进行WCF集成测试,启动WCF主机,发送消息并测试响应。这当然不是浪费时间。