4

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>
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • So, is it working for you now? I recently posted [this question](http://stackoverflow.com/questions/28192428/how-do-i-include-contract-assemblies-in-the-nupkg-automatically). I can't in the current process reference hardcoded paths like that in the nuspec file. I'd like to instead let nuget discover the contract assemblies automatically. – julealgon Jan 28 '15 at 13:05

3 Answers3

4

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.

Xavier Decoster
  • 14,580
  • 5
  • 35
  • 46
  • It's not the answer I hoped for -- a bit more work than it sounds like this should require -- but these seem to be the only option. Thanks! – Cory Nelson Dec 10 '12 at 18:14
1

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.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • 1
    It appears to be broken currently. The Contracts in the lib folder are not being copied over, they're being ignored. – nicodemus13 Oct 23 '13 at 18:51
0

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" />
OzBob
  • 4,227
  • 1
  • 39
  • 48