title: How can I make Code Contracts play nicely with a WiX Custom Action project?
body: <p>I have run into a problem in a solution containing a WiX manged custom action project, which is also using MS RiSE Code Contracts. The problem is that the code contracts tools fail with the following error:
  <img src="https://i.stack.imgur.com/MOgAQ.png" alt="enter image description here"></p>
  
  <p>I found a few mutterings about this issue going back at least 5 years, but not even a hint of a solution or workaround.</p>
  
  <p>As an experiment, I tried commenting out the WiX-specific targets as follows:  </p>
  
  <pre><code>  &lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;
    &lt;!--&lt;Import Project="$(WixCATargetsPath)" /&gt;--&gt;
    &lt;Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /&gt;
  </code></pre>
  
  <p>This allows the build to complete and the code contracts analysis/rewrite to complete successfully, however it obviously doesn't produce the correctly packaged self-extracting custom action. Since Windows Installer doesn't have any facility for managed code custom actions, WiX uses the trick of wrapping the managed code in an unmanaged wrapper which is a self-extracting archive containing the managed code custom action and its dependencies. It is a clever trick that lets us write custom actions in managed code, and it works very nicely. Until now.</p>
  
  <p>I suspect what is happening is that the WiX build process is stuffing the managed code into an unmanaged wrapper, which becomes the project's primary output and which the Code Contracts utility then chokes on, because it is not managed code. I can't be certain because the code contracts build process isn't visible, so I can't see how it is working.</p>
  
  <p>The above experiment makes me think that if I could somehow split the build process into separate parts, compiling the custom action code as a bog standard C# class library project, then doign the WiX packaging in a post-build event, I think the code contracts utilities would be able to process the code correctly. The problem is that I don't know enough about the Code Contracts mechanisms, or the WiX build process for that matter, to figure out what I'd need to do in the post-build event.</p>
  
  <p>Would it be possible to somehow perform the WiX custom action packaging in a batch file or PowerShell script, which could be run as a post-build event? What would I need to do?</p>