You've hit on a common point of confusion with .NET and F#. You're correct in observing that the documentation can be a bit ambiguous. Here's the breakdown of how self-contained mode works in your scenario, and why you're seeing the error:
The Core Issue: Self-Contained vs. Framework Dependency
- Self-Contained Executable: This means your application includes all the necessary runtime components (like .NET Core/ .NET runtime) within the executable itself. It's designed to run without requiring the .NET runtime to be installed on the target machine. This is ideal for standalone desktop applications and often preferred for WebAssembly.
- Framework Executable: This means your application relies on the .NET runtime being installed on the target machine. It's smaller in size because it doesn't include the runtime. This is common for web applications deployed to servers.
How it Works with Your Projects
You have two projects:
- Desktop Project: This is likely configured to be a self-contained executable. This is the standard for desktop applications.
- WebAssembly Project: This is configured to be a self-contained executable. This is the standard for WebAssembly applications. Crucially, it references the desktop project.
Why You're Getting the Error
The error message "The referenced project '..\LiveExtracted\LiveExtracted.fsproj' is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable." is correct. Here's why:
- The WebAssembly project is trying to reference a self-contained desktop project. The .NET SDK enforces this restriction. A self-contained executable cannot directly reference another self-contained executable. This is because the runtime dependencies are tightly coupled. The WebAssembly project needs to be able to run independently, and relying on the desktop project's runtime would break that independence.
The Solution: Framework Mode for the WebAssembly Project
The fix is to configure your WebAssembly project to be a framework executable. This allows it to reference the desktop project, which is a self-contained executable.
How to Change the Mode
- Open the
.fsproj file for your WebAssembly project.
- Find the
<TargetFramework> element. It will likely be something like <TargetFramework>net8.0</TargetFramework>.
- Change the
<TargetFramework> to <TargetFramework>net8.0-windows</TargetFramework> (or the appropriate target framework for your desktop project). This tells the project to build a framework executable.
- Ensure the desktop project is still configured as a self-contained executable. This is usually the default for desktop applications.
Important Considerations
- Dependencies: When you switch the WebAssembly project to framework mode, you'll need to ensure that all the dependencies used by both projects are compatible with framework mode. This is usually not a problem, but it's something to be aware of.
- Publishing: When you publish the WebAssembly project, you'll need to publish it as a framework web assembly application. This means it will rely on the .NET runtime being present on the target machine.
- Build Process: The change to framework mode will affect the build process. The WebAssembly project will no longer be able to be built as a standalone executable.
In summary:
- Self-contained mode is set during publishing, but the project's target framework determines whether it can be self-contained in the first place.
- You cannot reference a self-contained executable from another self-contained executable.
- To resolve your error, set the WebAssembly project to framework mode.
Example .fsproj snippet (WebAssembly project - Framework Mode):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WebAssembly</OutputType>
<TargetFramework>net8.0-windows</TargetFramework> <!-- Changed to framework mode -->
<PublishSingleFile>false</PublishSingleFile>
<SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<WebAssemblyTarget>WasmWasm</WebAssemblyTarget>
</PropertyGroup>
</Project>
After making this change, rebuild your solution. The error should be resolved. Remember to publish the WebAssembly project as a framework web assembly application.