views:

323

answers:

2

I have an issue with log4net which has been bugging me for a while and I've resolved to sort it.

I have a class library which references log4net. If I reference this class library in another project I must then reference log4net in this project otherwise I get a build error

Unknown build error, 'Cannot resolve dependency to assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' 

I'm aware that the error message is probably telling me the solution, unfortunately I don't speak gibberish...

Cheers guys

Alex..

+1  A: 

Here is the link which describes what is happening and how to fix it:

http://blogs.msdn.com/jmstall/archive/2006/11/22/reflection-type-load-exception.aspx

An excerpt from the above link:

So what happened was that it tried to get the System.Type for Bar, but to resolve the type it needs to load the base class, which is in another dll. Reflection-Only context doesn't do binding policy so it can't find that dll. The LoaderException hint says to use the ReflectionOnlyAssemblyResolve, which provides more information about this.

To use the reflection API, you have to resolve all the dependencies used.

Kevin
No, because this error occurs once I add my class library to an blank project, before I even make any calls into the class library.
Alex DeLarge
@Alex DeLarge I've linked to an msdn article that explains what is happening.
Kevin
Thanks for that Kevin, I'm gonna accept this answer but was looking more for how to specifically resolve this with log4net.
Alex DeLarge
I'm also getting this issue with log4net..wondering if its a VS2010->VS2008 issue as some people on the team are using 2010 already.
Luke
A: 

I had the same problem. I still don't fully understand it, but I can tell you how I solved my problem. I had a unit test project B with a project reference to project A, which references log4net. So to me the gibberish means when Visual Studio is trying to create the .accessor file for the unit test project, it reflects on project A. This means it tries to load project A's references, but the assembly loader can't find it because I don't have log4net in the GAC, just locally for project A to reference. In my case, adding log4net to my DEVPATH (GAC would work, too) was the solution.

BuilderBee