Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Reference "Copy Local" True / False Being Set Based on Contents of GAC

We had a very interesting problem with a Win Forms project. It's been resolved. We know what happened, but we want to understand why it happened. This may help other people out in the future who have a similar problem.

The WinForms project failed on 2 of our client's PCs. The error was an obscure kernel.dll error. The project ran fine on 3 other PCs.

We found that a .DLL (log4net.dll - a very popular open-source logging library) was missing from our release folder. It was previously in our release folder. Why was it missing in this latest release?

It was missing because I must have installed a program on my Dev box that used log4net.dll and it was added to the Global Assembly Cache.

When I checked the solution's references for log4net.dll, they were changed to "copy local=FALSE". They must have changed automatically because log4net.dll was present in my GAC.

Here's where my question starts:

Why did my reference for log4net.dll get changed from COPY LOCAL = TRUE to COPY LOCAL = FALSE? I suspect it's because it was added to my GAC by another program.

How can we prevent this from happening again? As it stands now, if I install a piece of software that uses a common library and it adds it to my GAC, then my SLNs that reference that DLL will change from Copy Local TRUE to FALSE.

like image 491
D-Sect Avatar asked Mar 20 '10 00:03

D-Sect


People also ask

What is GAC in C#?

Each computer where the Common Language Runtime is installed has a machine-wide code cache called the Global Assembly Cache. The Global Assembly Cache stores assemblies specifically designated to be shared by several applications on the computer.

What does Copy Local do in Visual Studio?

As a general rule you should use CopyLocal=True if the reference is not contained within the GAC. Copy Local essentially means I must manually deploy this DLL in order for my application to work.

How do you add references to a project?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference.


2 Answers

That happened because it doesn't make any sense to have Copy Local = True if an assembly is installed in the GAC. Because that local copy will never be used, the GAC is always searched first. Leaving it unchanged would cause major confusion, you'd think you are using the local copy but get another one instead. Changing it causes a confusion too, if you don't notice it, that could perhaps have been addressed with a message box at solution load time.

Log4net is a troublemaker, there are way too many versions in the wild without any deployment procedure that ensures that those versions don't bite each other. Something Apache apparently just didn't want to address, leaving it up to the programmer instead. Having products that take a dependency on Log4net and do something about the perceived DLL Hell risk is somewhat inevitable. Giving you a DLL Hell problem in return.

No clean simple answers, beyond being aware what's installed on your machine. Consider posting to connect.microsoft.com to ask for a warning when Visual Studio automatically updates the Copy Local property. It is a reasonable ask.

like image 92
Hans Passant Avatar answered Oct 01 '22 02:10

Hans Passant


I found a solution for us on our build servers.

Problem: In a specific date for no reason (I now assume the file was added to the GAC) the assembly System.Web.MVC.dll disappeared from many of our projects. I am sure this applies to any dll with the issue.

Solution: In visual studio change the reference (assuming Copy Local is already True).

  1. Change Copy Local = True to Copy Local = False
  2. Change Copy Local Back to True
  3. Notice in the .csproj file a <Private>True</Private> is added.
  4. Compile now and you will see the dll does indeed get included even though the message remains the same.

Solution2: Add the <private>True</private> property manually.

Conclusions: My understanding of <Private> is it is the Copy Local so if Copy Local = True the Private property should be added, but in our case at least it was not.

Additional Considerations:

  1. Tested using Visual Studio 2013 and 2015.
  2. Tested using msbuild 12 and 14.
  3. Tested using TFS build Template.
  4. Our code has been upgraded from 2008 to 2015 visual studio, the flag may have gotten messed up somewhere in that time, but the issue did not start until the assembly was added to the GAC somehow.

End Result:

<Reference Include="System.Web.Mvc, Version=4.0.0.1, Culture=neutral,PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
 <HintPath>packages\Microsoft.AspNet.Mvc.4.0.40804.0\lib\net40\System.Web.Mvc.dll        </HintPath>
        <Private>True</Private>
    </Reference>
like image 29
Metaphysico Avatar answered Oct 01 '22 03:10

Metaphysico