Monday, March 10, 2014

Unit Tests and Microsoft Practices Logging

I created a WCF Web Service where I am logging errors using the Microsoft.Practices.EnterpriseLibrary tools for handling exceptions and logging.
When I called the web service using the WCF Test Client, all was well and I recorded the errors successfully.

But when I added some unit tests that would also generate an exception when calling the web service, I received the following error:

Microsoft.Practices.ServiceLocation.ActivationException : Activation error occured while trying to get instance of type LogWriter, key ""
 ----> Microsoft.Practices.Unity.ResolutionFailedException : Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.


I spent some time Googling and finally found this post which explains all.
You need to copy the relevant sections of the web.config that refer to Enterprise Library into the app.config of the Unit Test Project. 

This also applies if you have an AppSettings entry (e.g. a Dynamics CRM Connection string) as this also needs to be copied into the app.config.