Search code examples
.netnugetcode-contracts

Controlling Code Contract references in a .nuspec


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>

Solution

  • 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.