Microsoft Code Contracts without Visual Studio

一个人想着一个人 提交于 2019-12-03 09:45:11

问题


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 Studio 2010. We're trying to do the same. We've followed the steps outlined in the accepted answer, but haven't been able to get it working.

CodeContracts will not install on the build server unless Visual Studio is present. So following the suggestion, we've done the following:

  1. We copied the contents of %programfiles%\Microsoft\Contracts\Bin from a development machine with Visual Studio 2010 Ultimate and Code Contracts Premium installed to the build server.
  2. We also copied the MSBuild\v4.0 folder that contains Microsoft.CodeContracts.targets and Microsoft.CodeContractAnalysis.targets.

According to the CodeContracts documentation,

Using msbuild on a project or solution that uses contracts enabled via the VS user interface will perform the same actions as the corresponding build under VS.

This is our use case, we're simply calling MSBuild on our solution file as below. The solution file is created via Visual Studio with all the expected Code Contract options configured for rewriting.

<Target Name="Release">
  <MSBuild Projects = "Cofamilies\WebApplication\CofamiliesWeb.sln" Properties="Configuration=Release" />
</Target>

But the rewriter is not getting called.

Does anybody have a suggestion for what we're missing and/or suggested troubleshooting steps?


回答1:


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.




回答2:


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"/>


来源:https://stackoverflow.com/questions/4291254/microsoft-code-contracts-without-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!