![](/img/trans.png)
[英]Is it possible to build an MsTest project into a self-contained executable? How?
[英]The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable
在通過 Visual Studio 調試時,在將 SDK 更新為 .NET 6.0 預覽后嘗試編譯我的 .netcore3.1 時遇到問題。
我的 CSProject 如下:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<UserSecretsId>f5884bc9-1eeb-1458-bba9-832ed8f4cd4e</UserSecretsId>
</Project>
我的 do.net sdk 安裝:
.NET SDK (reflecting any global.json):
Version: 6.0.100-preview.5.21302.13
Commit: d6380bcae7
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100-preview.5.21302.13\
Host (useful for support):
Version: 6.0.0-preview.5.21301.5
Commit: ec3e0b276b
.NET SDKs installed:
1.1.14 [C:\Program Files\dotnet\sdk]
2.1.519 [C:\Program Files\dotnet\sdk]
2.2.104 [C:\Program Files\dotnet\sdk]
3.0.103 [C:\Program Files\dotnet\sdk]
3.1.300 [C:\Program Files\dotnet\sdk]
5.0.301 [C:\Program Files\dotnet\sdk]
6.0.100-preview.5.21302.13 [C:\Program Files\dotnet\sdk]
錯誤信息:
引用的項目是一個非獨立的可執行文件。 自包含的可執行文件不能引用非自包含的可執行文件。
要解決此問題,所有引用的項目都必須是獨立的,並從所有項目中刪除平台標簽。 單擊解決方案中的每個項目並確保 *.csporoj 文件具有以下設置。
通用.csproj
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
</PropertyGroup>
AppWorker.csproj
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
應用程序.csproj
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\AppWorker\AppWorker.csproj">
<CopyLocal>True</CopyLocal>
<CopyLocalSatelliteAssemblies>True</CopyLocalSatelliteAssemblies>
</ProjectReference>
</ItemGroup>
還有一個重要的步驟,在進行 Publish 操作時,確保 select Release-AnyCPU 。 由於某種原因,其他選項使發布失敗並在工具欄中顯示錯誤的配置錯誤。
根據文檔,您的配置從未起作用,但您只會在嘗試運行引用應用程序時發現:
在以前的 .NET SDK 版本中,您可以從非自包含可執行項目引用自包含可執行項目,而不會出現構建錯誤。 但是,這兩個應用程序都無法運行。 從 .NET SDK 5 開始,如果一個可執行項目引用另一個可執行項目並且 SelfContained 值不匹配,則會生成錯誤。
現在,您在構建時收到錯誤,以便您知道您需要修復一個或另一個項目,以便它們的“自包含”匹配。
請注意,如果您確定要混合SelfContained
值,盡管存在潛在問題,您可以通過將<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
到 .csproj 文件來禁用對該條件的檢查。
我嘗試發布的 web 項目引用了另一個基礎 web 項目。
我在發布過程中遇到了同樣的錯誤。 我從我引用的項目的屬性部分將Output 類型“部分更改為Class 庫。
注: https://learn.microsoft.com/en-us/do.net/core/compatibility/sdk/5.0/referencing-executable-generates-error
用這個鏈接解釋不能完全解決我的問題。 在此過程之后,我在發布時遇到了不同的錯誤。
我用上面的解決方案解決了它。
我對 .net 3.1 而不是 SDK5 有同樣的問題。 我認為他們已經修復了一些問題,因為我的應用程序在生產和測試中非常愉快地工作,現在抱怨這個。
我將發布設置更新為依賴於框架,現在按預期工作。 當我保存文件時,Visual Studio 才剛剛開始構建,它從來沒有這樣做過。 也許是 VS 更新,因為我會自動執行這些操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.