Failed to add NuGet package

后端 未结 2 2020
盖世英雄少女心
盖世英雄少女心 2021-01-18 10:15

I have created a NuGet package libtidy which is pushed to a Team Services feed.

When I try installing via the NuGet console I get the error

相关标签:
2条回答
  • 2021-01-18 10:43

    You are probably getting this

    Failed to add reference to 'libtidy'

    because you have libtidy somewhere inside the lib folder. Installing a packag in this folder will automatically have a reference added to it, and you can't add a reference to an unmanaged library directly.

    If you are not already, you should look into manually creating your nuget package rather than doing it by project or assembly. To do this:

    1. Create a folder called nuget-libtidy
    2. Add libtidy.dll, TidyManaged.dll and anything else you need into this folder
    3. Open cmd and navigate to the folder you just created
    4. Run nuget spec to create the default manifest Package.nuspec (you will need to have nuget added to your PATH environment variable
    5. Add the following xml to the nuspec file just created right after the </metadata> closing tag

      <files>
          <file src="libtidy.dll" target="content" />
          <file src="TidyManaged.dll" target="lib" />
      </files>
      
    6. Adjust any of the other values you need.

    You could call it done and pack and deploy the nuget package. You need libtidy.dll to be copied to the output directory. This means after you install the package, you would have to navigate to right-click on dependencies\libtidy.dll in visual studio, select properties and set Copy to Output Directory to Copy Always.

    If you don't want everyone to have to do this you could make a few more adjustments to you nuget-libtidy folder and the manifest file. Basically you need to do is create an Install.ps1 file that adds

    <ItemGroup>
        <Content Include="libtidy.dll">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
    

    to the project file of the installed project.

    Here is an example of Install.ps1 that should do what you want:

    param($installPath, $toolsPath, $package, $project)
    
    $file = $project.ProjectItems.Item("libtidy.dll");
    
    If ($file -eq $null)
    {
        $project.ProjectItems.AddFromFile("libtidy.dll");
        $file = $project.ProjectItems.Item("libtidy.dll");
    }
    
    $file.Properties.Item("CopyToOutputDirectory").Value = [int]1;
    

    Once your PowerShell script is done add another line to you manifest file:

    <file src="Install.ps1" target="tools" />
    

    Don't forget running scripts on Windows 10 requires you to set the execution policy. I would suggest running Set-ExecutionPolicy RemoteSigned. After you run this in PowerShell, you will have to reboot your system.

    At this point, you should be able to pack and deploy.

    EDIT

    Typo found in Install.ps1 file. Line 3, $file1 = $project.ProjectItems.Item("libtidy.dll"); should be $file = $project.ProjectItems.Item("libtidy.dll";

    0 讨论(0)
  • 2021-01-18 11:08

    I once had an issue similar where I had to be running VS with elevated permission to install a nuget package. Something to do with our corporate security settings.

    0 讨论(0)
提交回复
热议问题