2016-01-10 28 views
1

我有一個項目。在這個項目中,我使用.NET的代碼合同。我設置運行時檢查= true。 。但在運行時我有一個ContractException:即使運行時檢查= true,彙編也不會被ccrewriter重寫

拋出異常: 'System.Diagnostics.Contracts.ContractException' 在 mscorlib.dll中

信息:一個組件(可能是 「通用」)必須 改寫使用的代碼收縮二進制重寫(CCRewrite),因爲 它所調用Contract.Ensures和CONTRACTS_FULL符號是 定義。刪除項目中的CONTRACTS_FULL符號 的任何顯式定義並重建。 CCRewrite可以從 http://go.microsoft.com/fwlink/?LinkID=169180下載。

安裝重寫器後,可以在代碼合同窗格的項目「屬性」頁面的Visual Studio 中啓用它。確保 說:「執行運行時合同選中」已啓用,它將定義 CONTRACTS_FULL 異常的原因是不是條件不符合,但該組件沒有被改寫。 Ccrewriter已安裝,我沒有其他項目的麻煩。

它發生在哪裏代碼:

Contract.Ensures(_instance != null && _instance._authData != null); 

這個代碼不被改寫,因此在執行過程的開始。

我知道有一個解決方法,只是用postbuild事件執行ccrewrite,但我想,以避免它。

什麼可以對這種行爲的原因是什麼?我如何檢查ccrewrite是否被調用?我在構建輸出中看不到任何信息。

回答

1

看起來它是在CC的錯誤。我發現了一個奇怪的workaroud:

我的項目

的csproj文件是由手之前編輯的,所以它與signAssebly標籤和codecontracts設置部分:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 
    <SignAssembly>true</SignAssembly> 
</PropertyGroup> 
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
    <SignAssembly>false</SignAssembly> 
    <CodeContractsEnableRuntimeChecking>True</CodeContractsEnableRuntimeChecking> 
    <CodeContractsRuntimeOnlyPublicSurface>False</CodeContractsRuntimeOnlyPublicSurface> 
    <CodeContractsRuntimeSkipQuantifiers>False</CodeContractsRuntimeSkipQuantifiers> 

    .... 

    <CodeContractsRuntimeCheckingLevel>Full</CodeContractsRuntimeCheckingLevel> 
    <CodeContractsReferenceAssembly>DoNotBuild</CodeContractsReferenceAssembly> 
    <CodeContractsAnalysisWarningLevel>3</CodeContractsAnalysisWarningLevel> 
    <RunCodeAnalysis>false</RunCodeAnalysis> 
</PropertyGroup> 

所以我搬到CodeContracts設置爲部分它在哪裏通常儲存;

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
    <PlatformTarget>AnyCPU</PlatformTarget> 
    <DebugSymbols>true</DebugSymbols> 
    <DebugType>full</DebugType> 
    <Optimize>false</Optimize> 
    <OutputPath>bin\Debug\</OutputPath> 
    <DefineConstants>DEBUG;TRACE</DefineConstants> 
    <ErrorReport>prompt</ErrorReport> 
    <WarningLevel>4</WarningLevel> 
    <Prefer32Bit>false</Prefer32Bit> 
    <CodeContractsEnableRuntimeChecking>True</CodeContractsEnableRuntimeChecking> 
    <CodeContractsRuntimeOnlyPublicSurface>False</CodeContractsRuntimeOnlyPublicSurface> 
    <CodeContractsRuntimeThrowOnFailure>True</CodeContractsRuntimeThrowOnFailure> 

    .... 

    <CodeContractsReferenceAssembly>%28none%29</CodeContractsReferenceAssembly> 
    <CodeContractsAnalysisWarningLevel>0</CodeContractsAnalysisWarningLevel> 
</PropertyGroup 

現在它的工作。我認爲在哪些地方放置版塊沒有太大的不同,它只對構建目標很重要。

如果有人可以解釋,爲什麼它會發生,我會重新接受了答案。

相關問題