views:

962

answers:

3

I have a CommonUtils lib I have built into a dll which I file reference from several of my projects. CommonUtils depends on log4net.dll which was set as a file reference and copy-local=true when CommonUtils.dll was built. log4net.dll and CommonUtils.dll are not in GAC.

Everything works fine in MyWorkingProject where I only have a file reference to CommonUtils.dll - log4net.dll shows up in the output directory (as it is a dependency of CommonUtils.dll but not referenced from MyWorkingProject). However if I create a new project to add some unittests: MyWorkingProjectTest and project reference MyWorkingProject and then file reference CommonUtils.dll again here (I want to use some of the CommonUtils) then upon building log4net.dll which is a dependency in both the project reference and in the file reference as well does not get copied to the output directory.

Can anyone clarify what is happening here? Does the compiler not know which of the log4net.dll files it should grab? Is there something I am doing wrong here? Should I do this in some other way for the log4net.dll to get copied explicitly? Does it make any sense that I have to explicitly reference log4net does it?

+1  A: 

I believe if you do not add log4net as a reference in your project it will not get copied.

sixlettervariables
How would you add log4net as a project reference as I only have the distributed binary dll. Also why would I have to add a reference to it as it is simply a dependency of one of my references. Someone stated that you should not reference libraries you are not explicitly using in your code.
Fadeproof
I didn't mean as a "Project Reference" rather a "Reference" in your project.
sixlettervariables
A: 

When I've had a similar situation in the past, I received a compiler warning within Visual Studio telling me that I needed to add a reference to the dependent library.

Are you receiving such a warning when you compile? If so, follow the advice it gives and you should find Log4net copied to your output directory.

Bevan
Problem is that I have two references that have dependencies on log4net and it seems that there is some built in thing to visual studio to not copy the conflicting dependency (dispite it being the same one). Everything works as expected if I only reference 1 but its not copied if i reference both. ?
Fadeproof
Are both references truly to the same assembly? Check the properties of each reference to ensure the path is identical.
Bevan
+1  A: 

This is by design. You need to think about the implications that automatic inheritance of references would cause. While you can logically see what the references should be, there is no such delineation to the compiler. You could, theoretically, end up reproducing the Framework and a good part of your OS if it walked the entire dependency tree.

It definitely is a pain, and I agree with and understand your frustration, but I assume that Microsoft was not able to find a logical way to do this otherwise.

joseph.ferris
Can you advice on how I should do this then? If I have LibA and LibB that both depend on log4net and are located in MyProjDir/LibA and MyProjDir/LibB that both contain log4net.dll. Which log4net.dll should I reference? Is copy-local true for log4net.dll not the way to go when building libA and libB?
Fadeproof
I personally add any shared references to the top-level project. For example, if both your libraries will be used by and application, I would add the log4net.dll directly to the /bin folder of the application. It is not the best solution, but I find it is easier than hunting the reference.
joseph.ferris
"by and application" = "by an application". Also, by saying that I add the references to the top-level is deceiving. I don't add a reference - I just add to the /bin folder since it will be in the probing path for the libraries looking for it.
joseph.ferris
Ok I see so I could add a post build task of sorts to copy it explicitly instead of building my CommonUtils with the log4net reference set to copy local = false?
Fadeproof