引用的项目是一个非独立的可执行文件。非独立可执行文件不能被独立可执行文件引用

ari*_*efs 24 .net c# asp.net .net-core

在通过 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>
Run Code Online (Sandbox Code Playgroud)

我安装的 dotnet 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]
Run Code Online (Sandbox Code Playgroud)

错误消息:

引用的项目是一个非独立的可执行文件。非独立可执行文件不能被独立可执行文件引用。

Pet*_*iho 17

根据文档,您的配置从未起作用,但您只有在尝试运行引用应用程序时才会发现:

在以前的 .NET SDK 版本中,您可以从非独立可执行项目引用独立可执行项目,而不会出现生成错误。但是,这两个应用程序都无法运行。从 .NET SDK 5 开始,如果可执行项目引用另一个可执行项目并且 SelfContained 值不匹配,则会生成错误。

现在,您在构建时收到错误,这样您就知道需要修复一个项目或另一个项目,以便它们的“独立性”匹配。

请注意,如果您确定要混合这些SelfContained值,尽管存在潜在问题,您可以通过添加<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>到 .csproj 文件来禁用对该条件的检查。

  • 每次进行重大更改时,他们似乎都喜欢告诉我们“您的配置从未起作用”,即使以前一切正常。 (14认同)
  • 那么,什么是项目的“独立性”?...它是发布配置文件的属性,不是吗? (7认同)
  • 有人知道我如何将“SelfContained”设置为 true 以使其匹配? (4认同)

rea*_*Pro 8

要解决此问题,所有引用的项目必须是独立的,并从所有项目中删除平台标签。单击解决方案中的每个项目,并确保 *.csporoj 文件具有以下设置。

Common.csproj

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

应用程序.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>
Run Code Online (Sandbox Code Playgroud)

还有一个重要的步骤是,在执行 Publish 操作时,请确保选择Release-AnyCPU。由于某种原因,其他选项导致发布失败并在工具栏中显示错误的配置错误。