简体   繁体   中英

The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable

I'm having an issue regarding trying to compile my .netcore3.1 after updated the SDK to .NET 6.0 preview while debugging through Visual Studio.

My CSProject is bellow:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <UserSecretsId>f5884bc9-1eeb-1458-bba9-832ed8f4cd4e</UserSecretsId> 
    
</Project>

my do.net sdk installed:

.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]

error messages:

The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable.

To fix this, all the referenced projects, must be self-contained and remove the Platforms tag from all the projects. Click on every project in your solution and make sure that the *.csporoj files have the following setup.

Common.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>

App.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>

Also an important step, when doing the Publish operation, make sure to select Release-AnyCPU . For some reason the other options make the publish to fail and display bad configuration error in the toolbar.

According to the documentation , your configuration never did work, but you would only find out when trying to run the referencing application:

In previous .NET SDK versions, you could reference a self-contained executable project from a non-self-contained executable project without a build error. However, both apps would not be runnable. Starting in .NET SDK 5, an error is generated if an executable project references another executable project and the SelfContained values don't match.

Now, you get the error at build time, so that you know then that you need to fix one project or the other so that their "self-contained-ness" matches.

Note that if you are sure you want to mix the SelfContained values in spite of the potential issues, you can disable checking for that condition, by adding <ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained> to the .csproj file.

The web project I'm trying to publish is referencing from another base web project.

I got the same error during the publish process. I changed the Output Type "part to Class Library from the properties section of the project I referenced.

Referenced Project Properties: 在此处输入图像描述

Note: https://learn.microsoft.com/en-us/do.net/core/compatibility/sdk/5.0/referencing-executable-generates-error
Explained with this link could not solve my problem exactly. After this process, I got a different error in publish.
I solved it with the solution above.

I have the same issue with .net 3.1 and not SDK5. I think they have fixed something as my application was quite happily working in production and test, and now complains about this.

I updated the publishing settings to be framework dependant and it is now working as expected. Visual studio has just starting building when I save files, which it never used to do. Maybe a VS update as I do those automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM