针对WCF服务运行mstest时,WcfSvcHost无法运行且测试失败.调试时测试通过

And*_*dyM 5 wcf unit-testing

使用Visual Studio 2010,我编写了一个简单的WCF服务和一些我想针对它运行的集成测试.我在运行时使用代码而不是使用配置为测试构建代理.

我的测试在调试时传递,但在运行时没有!

如果运行失败 - 在当前上下文中进行测试/运行/测试(因为它调用的WCF服务尚未托管)

在调试中通过 - 在当前上下文中进行测试/调试/测试(因为在同一解决方案中调试另一个项目时,WCF项目具有WCF选项/启动WCF服务主机)

有没有办法在测试正常运行时启动WCFServiceHost?

谢谢,安迪

Test method BulkLoaderIntegrationTests.IntegrationTests.ImportEntries_withGoodPCMs_reportsCreatedOk threw exception: 
    System.ServiceModel.EndpointNotFoundException: Could not connect to net.tcp://localhost:8001/OLELoader. The connection attempt lasted for a time span of 00:00:00.9687686. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8001.  ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8001
Run Code Online (Sandbox Code Playgroud)

And*_*dyM 4

在调试同一解决方案中的另一个项目时,我禁用了“启动 WCF 服务主机”。

我在 [ClassInitialize] 中添加了一个静态方法,以便在测试期间在测试上下文中“自行托管”WCF 服务。

        [ClassInitialize]
        public static void Init(TestContext t)
        {
            IntegrationTests.InitService();
        }

        [ClassCleanup]
        public static void CleanUp()
        {
            IntegrationTests.host.Close();         
        }

        private static bool ServiceIsStarted = false;
        private static ServiceHost host;
        private static void InitService()
        {           
            if (!ServiceIsStarted)
            {
                // Create the ServiceHost.
                host = new ServiceHost(typeof (OLEImport),
                                           new Uri(IntegrationTestHelper.BaseAddress));

                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                host.Open();
                ServiceIsStarted = true;
            }
        }
Run Code Online (Sandbox Code Playgroud)