log4net 是否会杀死我的 WCF 单元测试?
我的解决方案中有三个项目:
- 提供我想要测试的功能的 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),它按预期显示 - 信息和指向 wsdl 的链接。
当我使用网络应用程序时,一切正常。该代码调用 Web 服务并获得正确的回复响应。日志记录通过 Web 应用程序和 Web 服务进行。
但是,当我运行单元测试时,它们都失败并出现以下异常:
测试方法 MyServiceTest.MyServiceAuthTest.TestValidateCorrectly 抛出异常:System.ServiceModel.ServiceActivationException:请求的服务,'http://localhost/MyWebService/MyWebService .svc' 无法激活。有关详细信息,请参阅服务器的诊断跟踪日志。
在我的本地计算机的事件日志中,我收到以下消息:
WebHost 无法处理请求。 发件人信息:System.ServiceModel.ServiceHostingEnvironment+HostingManager/12905972 异常:System.ServiceModel.ServiceActivationException:由于编译期间出现异常,无法激活服务“/MyWebService/MyWebService.svc”。异常消息为:无法加载文件或程序集“log4net,Version=1.2.10.0,Culture=neutral,PublicKeyToken=1b44e1d426115821”或其依赖项之一。无法验证强名称签名。该程序集可能已被篡改,或者已延迟签名但未使用正确的私钥完全签名。 (HRESULT 异常:0x80131045)。
我删除并替换了引用,清理并重建,甚至清除了临时 asp.net 文件,但都无济于事。该网站调用该服务没有任何问题,但测试失败。
有谁知道这里会发生什么?
更新:我删除了所有对 log4net 的引用,测试运行并成功,没有出现任何问题。显然,这远非可取的。有什么建议吗?
更新 2:这两件事的某种组合解决了问题:
- 在我的测试项目中添加了对 log4net 的引用,确保将其完全初始化。
- 使用 log4net 1.2.10 的发布版本而不是调试版本。
I have three projects in my solution:
- A WCF web service which provides functionality I want to test
- A Web Application which calls that web service
- A test project which runs tests on the service.
The web service and the web application both use log4net with separate configuration files and this line in the AssemblyInfo.cs for configuration:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
When I browse to the URL of the web service (http://localhost/MyWebService/MyWebService.svc), It appears as expected - information and a link to the wsdl.
When I use the web application, it all works correctly. The code calls the web service and gets correct responses in reply. Logging occurs from both the web app and the web service.
However, when I run my unit tests, they all fail with the following exception:
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.
In the Event logs for my local machine, I get the following message:
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).
I've removed and replaced the references, cleaned and rebuilt, and even purged the temporary asp.net files, all to no avail. The web site calls the service with no problems, but the tests fail.
Does anyone know what could be happening here?
Update: I removed all references to log4net and the tests ran and succeeded without a problem. Obviously, this is far from preferable. Any suggestions?
Update 2: Some combination of these two things fixed the problem:
- Added a reference to log4net in my test project, making sure to initialise it completely.
- Used the release build of log4net 1.2.10 rather than the debug build.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的单元测试中有 log4net 库的参考吗?尝试一下。
其背后的原因是:大多数单元测试框架在测试运行期间将二进制文件隐藏到其他地方,但通过这样做,它们并不能在所有情况下找到深度嵌套的引用。我曾经在 NHibernate 和 log4net 中遇到过这个问题。如果是 MSTest,您无法停用阴影。 NUnit GUI 有一个选项,但我没有安装它,所以你必须自己寻找它:-)
Do you have a reference from your unit test to the log4net library? Try it out.
The reason behind this: Most unit testing frameworks are shadowing the binaries to somewhere else during the test run, but by doing this they do not find deeply nested references in all cases. I once met this with NHibernate and log4net. In case of MSTest you cannot deactivate shadowing. NUnit GUI has an option for this, but I don't have it installed, so you have to look for it on your own :-)
我还会考虑将 log4net 处理添加到您的单元测试配置中。无论如何,这在查看失败时非常有帮助。
这个链接解释了NUNit,它应该是对于其他框架也是可能的。
I would also consider to add log4net handling into your unit tests configuration. This is very helpful when looking at failures anyway.
This link explains it for NUNit and it should be possible for other frameworks as well.