Nick Proud
Nick Proud

Reputation: 395

'Could not load file or assembly. The system cannot find the file specified' - thrown after packaging .NET Framework NuGet package

I am trying to package a class library which references another DLL called 'TestDLL'

The target is .NET framework 4.8 and I am using NuGet Package Explorer to build the package. I have added a lib folder and a folder named after the target framework, in which I have the dll for the main project and then the referenced DLL which I have called 'TestDLL'

See below:

enter image description here

Here is the metadata (nuspec content) which contains a reference to TestDLL.dll

    <?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
  <metadata>
    <id>Nexbotix.OCR</id>
    <version>1.0.7</version>
    <authors>Nexbotix.OCR</authors>
    <owners></owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <dependencies>
      <group targetFramework=".NETFramework4.8" />
    </dependencies>
    <references>
      <group targetFramework=".NETFramework4.8">
        <reference file="TestDLL.dll" />
      </group>
    </references>
  </metadata>
</package>

I am packaging this up into a nupkg file from Package Explorer. Everything seems fine in that I am able to import the nuget package into my target project without error. Then when I go to run the code, if I attempt to reference an object from within TestDLL.dll, I get this error stating that the file for reference TestDLL.dll cannot be found.

enter image description here

Obviously there is something wrong with the way I am constructing the nuget package file structure. I simply want to package a class library in .NET Framework which itself references another DLL which is visible to whichever project imports the nuget package. Can anyone help point me in the right direction?

Upvotes: 0

Views: 2062

Answers (2)

Nick Proud
Nick Proud

Reputation: 395

I found the reason this is happening and it is not because of anything I was doing wrong in dependency management.

Instead, it was due to a problem in the original open-source project.

I reached out to the repository maintainer who explained that it is a known issue with dependencies and that there is a requirement for a specific project in the solution to also have a reference to the dependency until the issue is fixed.

Thank you to everyone who attempted to answer this question. Much appreciated.

Upvotes: 1

Sara Liu - MSFT
Sara Liu - MSFT

Reputation: 6218

Not sure if nuget cache caused this. Try the following steps:

1) uninstall your nuget package Nexbotix.OCR

2) clean nuget caches or just delete all files under C:\Users\xxx\.nuget\packages

3) when you pack your nuget project with the nuspec file, I suggest you should remove

  <references>
      <group targetFramework=".NETFramework4.8">
        <reference file="TestDLL.dll" />
      </group>
    </references>

When you used this, it will only reference TestDLL.dll and MyProject.dll will never be added into your main project. This is the result of my test. Whichever you specify in <references>, it will only install that.

Or, you should add all the dlls into references node like this:

 <references>
 <group targetFramework=".NETFramework4.8">
    <reference file="TestDLL.dll" />
    <reference file="MyProject.dll" />
  </group>
 </references>

In fact, if you do this too much, nuget will install all the content of the lib folder automatically without using the References node.

The whole nuspec file I used is this:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>xxx</id>
    <version>1.0.0</version>
    <title>me</title>
    <authors>me</authors>
    <owners>me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
    ....
    <tags>Tag1 Tag2</tags>
 <dependencies>
      <group targetFramework=".NETFramework4.8" />
    </dependencies>
  </metadata>
<files>
<file src="bin\Debug\TestDLL.dll" target="lib\net48" />
<file src="bin\Debug\TestDLL.pdb" target="lib\net48" />
<file src="bin\Debug\MyProject.pdb" target="lib\net48" />
</files>
</package>

Upvotes: 0

Related Questions