问题
I'm using Code Contracts to generate satellite assemblies for my project. Basically it creates a MyAssembly.Contracts.dll for the project's MyAssembly.dll. This is supposed to be put beside your assembly but not referenced by any app -- it's used only by the contracts tools.
I'm trying to include this in my nuget package, but when I then install that package into another app, the Contracts assembly is included as a reference which creates problems.
According to the .nuspec reference, this should be as simple as adding a references
element, but it seems to be ignored. My current theory is that this is because I'm using a .csproj for replacement tokens, and it's reading references from the .csproj and not the .nuspec.
Has anyone done this before? Any ideas?
Here are the relevant parts of my .nuspec:
<?xml version="1.0"?>
<package>
<metadata>
<references>
<reference file="MyAssembly.dll"/>
</references>
</metadata>
<files>
<file src="bin\Release\CodeContracts\*.dll" target="lib\net45"/>
</files>
</package>
回答1:
As Code Contracts are a separate and optional assembly, I'd recommend creating a new MyPackage.CodeContracts package which copies over the Code Contracts assembly to where you need it. This can be done using a simple PowerShell script (install.ps1 + uninstall.ps1) in a so called tools package.
This is a similar approach as for creating a MyPackage.Sample package for instance. Create the package using a convention based directory and target a .nuspec file instead. You'll have less issues and IMHO it's a nicer solution as well.
回答2:
Following up -- newer NuGet versions seem to do what I had originally expected it to do and include the files without referencing them. No more need for custom installers.
回答3:
This was an issue for me too. I have replicated it with the the Nuget Sample to: https://github.com/NuGet/Samples/issues/3
The package\metadata\references\group\reference node documentation: http://docs.nuget.org/docs/reference/nuspec-reference#Specifying_Explicit_Assembly_References_in_version_2.5_and_above
Someone else with the same issue: https://social.msdn.microsoft.com/Forums/en-US/fe7fad45-a474-4157-a4ee-3f94a2b1071f/nuget-package-dependency-reference-not-added-to-project?forum=visualstudiogeneral
Sample nuspec:
<references>
<reference file="My.ServiceBusEntities.dll" />
</references>
</metadata>
<files>
<file src="bin\Debug\My.ServiceBusEntities.dll" target="lib\net45\ServiceBusEntities.dll" />
NOTE: it is important to have the correct and matching "net45" framework folder in the file:target attribute to match the project target (i.e. framework 4.5).
Other wise have no framework specified and you will see references added to the Target Project. i.e.
<file src="bin\Debug\My.ServiceBusEntities.dll" target="lib\ServiceBusEntities.dll" />
来源:https://stackoverflow.com/questions/13687995/controlling-code-contract-references-in-a-nuspec