ChatGPT解决这个技术问题 Extra ChatGPT

Reference DLL file not copying to bin with deployment project, causing error

We have several external DLL files being referenced in our Web Application Project. We have a deployment project for installing on the hosting servers. When we were using .NET 3.5 and Visual Studio 2008 the DLL files were being copied to the bin folder. Since we have upgraded to .NET 4 and Visual Studio 2010 this no longer happens, and we are getting server errors since the references cannot be found.

CopyLocal is set to true, and I cannot find anything inside the web.config which suggests this is being set elsewhere.


P
Peter Mortensen

There is a bug in Visual Studio 2010. By default the XML in the solution file looks like this:

<Reference Include="DevExpress.SpellChecker.v11.1.Core,
           Version=11.1.5.0,
           Culture=neutral,
           PublicKeyToken=b88d1754d700e49a,
           processorArchitecture=MSIL">
<HintPath>..\References\DevExpress.SpellChecker.v11.1.Core.dll</HintPath>
</Reference>

Whereas MSBuild is expecting this below, so that the DLL file will be included in the deployment:

<Reference Include="DevExpress.SpellChecker.v11.1.Core,
           Version=11.1.5.0,
           Culture=neutral,
           PublicKeyToken=b88d1754d700e49a,
           processorArchitecture=MSIL">
<HintPath>..\References\DevExpress.SpellChecker.v11.1.Core.dll</HintPath>
<Private>True</Private>
</Reference>

The trick is to set Copy Local to False, save the project and then reset it to True - save again. This includes the Private node correctly, which MSBuild respects.

It appears that the default for no included private node (Copy Local) in Visual Studio 2010 is True, while MSBuild reads that missing node as False.


Bug is still there in VS2012. It really should be fixed, took me a while to find this solution.
Today I discovered that this CopyLocal trick does not affect the references above the current project. For example, I have a project Entities which references some.dll in GAC. If I force CopyLocal=true for some.dll as described above, everything seems fine - some.dll gets copied to the bin folder of Entities. But if I reference the Entities project in some project Services and expect to get some.dll copied to the Services\bin, it won't happen - Services project ignores CopyLocal=true of the Entities project and still grabs the some.dll from the GAC. Using VS2012.
Although this fixes the project file, this setting is still not respected by MSBuild.exe from the command line so any sort of automated build scripts will not be building the code correctly (e.g. the referenced project reference dll will not be in the bin directory)
I'm running into this is VS2013 when I configure nuGet to supply missing packages. The only way I've found to succesfully work around this is to add references to the missing assemblies to my starting project. Yecch.
I just had the same issue in VS2015. FYI, the XML references mentioned in this answer are stored in the project file, not the solution file.
s
stevie_c

I was getting the same problem and rather than add a "BeforeBuild" step I created a test that simply did this

    [TestMethod]
    public void ReferenceAssemblyThatDoesNotCopyToBuildFolder()
    {
        Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler referenceThisButDoNotUseIt = null;
    }

And that fixed the error The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler...' cannot be resolved


P
Peter Mortensen

Something weird had happened to my deployment project. When I saw it had no detected dependencies, I removed the primary output and re-added it.

The dependencies are now showing up and being placed in the bin folder when installed.


Great. just one question: How did you find out that it had no detected dependencies? which option is this in? please elaborate. thanks
In my deployment project there is a detected dependencies folder. The only thing it was detecting in there was the .net framework. You can right click on this folder and click "Refresh dependencies", but even this wasn't working for me. When i re-added the primary output the dependencies were detected. It was very odd, and was caused sometime during the conversion between vs2008 and vs2010. I'd say its a bug but for the fact that all my other deployment projects converted perfectly. At the end of the day i have no idea what has happened.
I had the same problem with a newly created solution in vs.net 2010, so it is not an upgrade issue. As you said, removing the primary output and readding seems to fix the issue.
could someone please clarify what "remove the primary output" means?
P
Peter Mortensen

I just had the same issue and wanted to share what I found as it might help someone:

The reason in my case was that the assembly was installed in the GAC during an installation of some third-party application.

If the DLL file is in the GAC, the compiler won't bother to copy it to the destination folder, unless you specifically mark it for "copy local" using the "Private" node in the project file as mentioned by Junto.

The thing is that if you don't add that node, and you develop on one machine and build on a different one, and the DLL file is only in the GAC of the build machine, the default behavior without the private node will cause the file to be copied correctly on the development machine, but not on the build machine.

The bigger problem is if the DLL file is not referenced directly, but the project references a second project that in turn references the DLL file. In that case, you cannot mark the DLL file to be "copy local" in the project, as it is not referenced by it. So if the DLL file exists in the GAC - it won't get copied to your output folder.

Possible solutions to this case are:

Uninstall the DLL file from the GAC

Add a direct reference to the DLL file in the end project(s)

Re-sign the DLL file with a new strong name, which will differentiate it from the DLL file in the GAC.


P
Peter Mortensen

I was getting exactly the same issue. We have a Visual Studio 2008 project which references the EnterpriseLibrary. When we run our integrated build using TFS and our Web deployment project, all the DLL files are copied over. When we upgraded to Visual Studio 2010, TFS 2010 and WDP 2010, some of the DLL file's were missing. Strangely, this only occurs to some DLL files and not others.

For example, we get the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll copied in both cases, but not the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll.

As a workaround I copied the files accross using a "BeforeBuild" step.

It now seems to build OK.


B
Bronek

I didn't meet the same problem but similar. I had WPF main project and referenced project where the referenced did not copy. I found that in my case the main project was set for NET 4.0 Client Profile and the referenced for NET 3.5. When I set the main project to 3.5 the compiled dll of the referenced project started to copy. (I don't know why because I solved it by practice)


u
user4869201

I too ran into a similar issue where referenced dlls were not copied into the bin in published folder. I was using a TFS checked out copy that didn't include the bin folder into the application. -> So just included the bin folder. -> Built the referenced applications -> Published the website project Now I see all the referenced dlls in bin in the published folder


c
chviLadislav

I had a similar issue with VS 2012 Express. I used Tesseract libraries in my project. Everything worked well until I used this project in a solution where were more than one project. Problem was that some DLLs (liblept168.dll, libtesseract302.dll) that are normally placed in folders bin/debug/x86 or bin/debug/x64 were copied only when I rebuilt whole solution. Changing a single line and building it again caused that the DLLs were deleted and not copied back.

I solved this issue by adding a reference of the project that creates missing DLLs to the startup project.


J
John H

rzen and others, thanks - your comments led to a solution for us.

We have a project that targets version 10 of the Microsoft.ReportViewer.Common.dll and Microsoft.ReportViewer.WebForms.dll assemblies (separate "libs" folder we created at the 'src' level). But when we did a build, the output included version 12, which was recently installed on the build server.

Using comments here, we ensured that 'Copy Local' was set to True and that the flag was set in the project file. However, it was still deploying version 12. So what we found that did the trick was ensuring that the 'Specific Version' property was also set on the two references. Voila, version 10 of each file is now being deployed!

There was much rejoicing.

JH


L
Lukos

If your project does not directly load the library, it won't always be deployed, even if it is referenced explicitly! I got confused because I could see it in a local Bin directory but not when deployed. The dll in the Bin directory was an old file that wasn't removed during Clean which is why I was confused.

A full clean and rebuild and it wasn't in my local Bin folder either which showed me the problem (I only use it in web.config). I then referenced the dll file itself in the project and set it to copy to output to make sure it gets deployed.


P
Peter Mortensen

I am not sure how it was set up in Visual Studio 2008, but I am almost positive that you might have been using the Post-Build event command line. In there you can tell to copy the DLL files you need for deployment. An example is given below:

mkdir $(SolutionDir)\Deployment
copy "$(SolutionDir)Your_Library_Name\Your_Dll_ForDeployement.dll" 
$(SolutionDir)\Deployment\

I check this and it wasn't the case, Thank you anyway tho.
P
Peter Mortensen

We can use the <Private>False</Private> to not to copy the referenced DLL files to the bin directory. This is useful when we are building applications in a separate TFS build server where we need to build the application and not to copy the DLL files to the bin directory.


P
Peter Mortensen

Check the framework of the project in which the DLL file has been referenced. The framework should be .NET 4.0. Please correct it if the framework is Client Profile.


G
Giles Roberts

Adding the parameter

/deployonbuild=false

to the msbuild command line fixed the issue.


C
Christian

Got a similar issue when upgrading old WebSites into WebApplications. The "Clean Solution" command would wipe out all external DLL files I purposely left in my bin folders.

Besides, it was not possible to bring those DLL back automatically simply by referencing them all, since many of them have the same file name (it happens when you work with many language specific resources)

Like stevie_c did, I took advantage of the Pre-Build command, but made it simpler:

I just used a xcopy command in the Pre-Build operation of the WebApplication project's properties. This way I could bring over the necessary external DLL files just before the build would start.