views:

282

answers:

2

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.
+3  A: 

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 :-)

Marc Wittke
This is a good explanation. Another workaround is to use other ways to configure log4net. For example, loading the config file in code instead, and this is preferred by me as it allows you to handle the file missing exceptional cases.
Lex Li
To be honest, I think I did that without success. I'm at home right now, but I'll definitely have another go when I get back to work. I may have missed a step at some point...
Damovisa
Not entirely sure what fixed the problem, but this definitely helped. I added references to the test project and instantiated it in init, but it still failed. I removed the reference to log2net debug and replaced it with a release version and everything worked. Version 1.2.10 by the way...
Damovisa
A: 

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.

weismat