OnSwipe redirect code

Sunday, November 25, 2007

VS2005 has a feature (supposedly) called "Copy Local"

VS2005 and .NET users are pretty familiar with the way the IDE makes the job so easy and keeps us away from all the build related things like references, linking and all that stuff. But in many cases this might actually be far from desired behavior. One such that recently took away some of my time was the "Copy Local" attribute on the references. This is a boolean property on the references added to a project. If this is set to true, then when the project is built the assemblies added as references as copied to the 'bin" folder of that project. Depending on the build, its either the "Debug" or the "Release" folder. With this, in future whenever the project is built, the DLLs in the bin directory are used, unless you change your references. This will cause a problem when one of the referred assembly is updated and you expect the new behavior, but get the old one instead.

Example, You would have added an assembly, E:\assemblies\MyAssembly.dll as a reference. Now when you want to update the assembly you typically replace the old MyAssembly.dll with the new DLL in the above mentioned location. And then you expect your project to reflect the new assembly, but it does not do that because "Copy Local" being true would have lead to the old DLL being copied to the bin folder of the project and though you updated the DLL in its original location, the reference was pointing to the DLL copied to the bin directory.

So watch out for this Feature. From what I know, this feature can be serialized in the assembly itself. Though this might be a good feature for applications which will use the same DLLs for a long time, this is clearly a bad choice for people who are writing small test applications to test their DLLs. Because with every new build the DLL version changes and yet the references point to the old one.


2 comments:

  1. Someone correct me if i am wrong, but if you are updating references to assemblies/libraries with Copy Local set to true, you can still replace the DLL in the original location.

    You just need to to 'Clean Solution' and build the solution for it to take effect. The Clean Solution effectively clears your bin folder.

    ReplyDelete
  2. @Sunny Blogger

    I think cleaning up the solution will work here as the DLLs have to be copied afresh from the original location. Just that one needs to be aware of the fact that the referenced assemblies are actually copied to the "bin" directory of the referencing project and they are not picked from their original location every time.

    ReplyDelete