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

时间:2010-09-08 10:57:43

标签: wcf unit-testing

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

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

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

调试中的PASS - 在当前上下文中进行测试/调试/测试(因为在同一解决方案中调试另一个项目时,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

1 个答案:

答案 0 :(得分: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;
            }
        }