Microsoft Code Contracts without Visual Studio

后端 未结 2 1012
无人共我
无人共我 2021-02-04 14:15

This stack overflow question:

Microsoft Code Contracts and CI build server

asks how to get code contracts working on a build server without installing Visual Stu

相关标签:
2条回答
  • 2021-02-04 14:24

    I had the same problem with the latest version of Code Contracts. I had installed the Premium Edition on my development PC and the Standard Edition on the build server and was getting the following error due to the Rewriter not running.

    Must use the rewriter when using Contract.Requires<TException>

    It appears that the Standard edition is missing a key file (CodeContractsAfter.targets) that is needed for MSBuild to invoke the Rewriter.

    The solution for me was to copy the CodeContractsAfter.targets from C:\Program Files (x86)\MSBuild\4.0\Microsoft.Common.Targets\ImportAfter on my development PC to the corresponding folder on the build server.

    Note: The paths weren't identical as my development PC is running Windows 7 64bit and the build server is running Windows Server 2003 32bit. So you'll need to figure out the exact paths for your environment.

    If you aren't using the Premium Edition, the contents of the CodeContractsAfter.targets file is:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <!-- Begin CodeTools: CodeContracts: After -->
      <PropertyGroup>
        <CodeContractsInstallDir Condition="'$(CodeContractsInstallDir)'==''">C:\Program Files (x86)\Microsoft\Contracts\</CodeContractsInstallDir>
      </PropertyGroup>
      <Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)MsBuild\v4.0\Microsoft.CodeContracts.targets" />
    
      <!-- End CodeTools: CodeContracts: After -->
    </Project>
    

    Just paste the above into a file in the folder mentioned ImportAfter folder.

    0 讨论(0)
  • 2021-02-04 14:44

    Here's my solution. It's based on issue #368 and stackoverflow:Microsoft Code Contracts without Visual Studio

    1. Add DotNet.Contracts from nuget;
    2. Add code below to your project file:
    <PropertyGroup>
      <CodeContractsInstallDir Condition="'$(CodeContractsInstallDir)' == ''">$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\packages\DotNet.Contracts.1.10.20606.1\'))</CodeContractsInstallDir>`
    </PropertyGroup>
    <Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)\MsBuild\v$(VisualStudioVersion)\Microsoft.CodeContracts.targets"/>
    
    0 讨论(0)
提交回复
热议问题