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

标签 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

最佳答案

在同一解决方案中调试另一个项目时,我禁用了“启动 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;
            }
        }

关于wcf - 针对 WCF 服务运行 mstest 时,WcfSvcHost 无法运行且测试失败。调试时测试通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666978/

相关文章:

python - @mock.patch 如何知道每个模拟对象使用哪个参数?

asp.net-mvc - MVC - 将 MVC 与 WCF 结合使用

javascript - 从 javascript 调用 WCF 服务

unit-testing - Angular 2 单元测试 - @ViewChild 未定义

java - Java 8 LocalDate 的 Hamcrest 匹配器

unit-testing - Rhino 模拟,MbUnit : Best way to check if object has raised an event

c# - 添加 WCF 服务会生成 Reference.cs 文件,其中引用 Microsoft.CodeDom 而不是 System.CodeDom

sql-server - Windows Azure 上的 WCF 服务 - 连接到本地 SQL Server 数据库

c# - 拖放不起作用

objective-c - 既然我正在使用 Core Data,如何对我的模型进行单元测试?