我的解决方案中有三个项目:
- 提供我想要测试的功能的 WCF Web 服务
- 调用该 Web 服务的 Web 应用程序
- 对服务运行测试的测试项目。
Web 服务和 Web 应用程序都使用 log4net 和单独的配置文件,并在 AssemblyInfo.cs 中使用此行进行配置:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
当我浏览到 Web 服务的 URL 时(http://localhost/MyWebService/MyWebService.svc http://localhost/MyWebService/MyWebService.svc),它按预期显示 - 信息和 wsdl 链接。
当我使用网络应用程序时,一切正常。该代码调用 Web 服务并获得正确的回复响应。日志记录通过 Web 应用程序和 Web 服务进行。
但是,当我运行单元测试时,它们都失败,但出现以下异常:
Test method MyServiceTest.MyServiceAuthTest.TestValidateCorrectly threw exception: System.ServiceModel.ServiceActivationException: The requested service, 'http://localhost/MyWebService/MyWebService.svc' could not be activated. See the server's diagnostic trace logs for more information.
在本地计算机的事件日志中,我收到以下消息:
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/12905972
Exception: System.ServiceModel.ServiceActivationException: The service '/MyWebService/MyWebService.svc' cannot be activated due to an exception during compilation. The exception message is: Could not load file or assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' or one of its dependencies. Strong name signature could not be verified. The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key. (Exception from HRESULT: 0x80131045).
我删除并替换了引用,清理并重建,甚至清除了临时 asp.net 文件,但都无济于事。该网站调用该服务没有任何问题,但测试失败。
有谁知道这里会发生什么?
Update:我删除了所有对 log4net 的引用,测试运行并成功,没有出现问题。显然,这远非可取的。有什么建议么?
Update 2:这两件事的某种组合解决了问题:
- 在我的测试项目中添加了对 log4net 的引用,确保完全初始化它。
- 使用 log4net 1.2.10 的发布版本而不是调试版本。