3

I'm trying to build a C# project, using WinForms (if that helps), under GNU/Linux using Docker and .NET Core Docker image, as explained in the documentation. Disclaimer : I'm not an expert in C# or .NET.

My project is structured like this :

.
├── MyProject.sln
└── MyProject
│   ├── Package1
│   |   └── ...
│   ├── Package2
│   |   └── ...
│   └── MyProject.csproj
└── MyProjectUnitTests
    ├── ...
    └── MyProjectUnitTests.csproj

In both .csproj files, the target framework version is v4.6.1 :

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-->...<-->
  <PropertyGroup>
    <!-->...<-->
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
  </PropertyGroup>
</Project>

I'm using the following Dockerfile, like in this official example :

FROM    mcr.microsoft.com/dotnet/core/sdk:3.1.201-buster

WORKDIR /app

# copy all files necessary to resolve dependencies
COPY    MyProject.sln .
COPY    MyProject/MyProject.csproj MyProject/
COPY    MyProjectUnitTests/MyProjectUnitTests.csproj MyProjectUnitTests/

# resolve dependencies
RUN     dotnet restore --verbosity normal

# copy all files
COPY    . .

RUN     dotnet build

The download of dependencies works correctly (dotnet restore), but the last command (dotnet build) failed with the following error :

Build FAILED.

/usr/share/dotnet/sdk/3.1.201/Microsoft.Common.CurrentVersion.targets(1177,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.6.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [/MyProjectUnitTests/MyProjectUnitTests.csproj]
/usr/share/dotnet/sdk/3.1.201/Microsoft.Common.CurrentVersion.targets(1177,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.6.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [/MyProject/MyProject.csproj]
    0 Warning(s)
    2 Error(s)

I don't understand the errors. The errors redirects me to a Microsoft website where I can find explanations for downloading and installing .NET Core 3.1.201, but that's exactly why I'm using the mcr.microsoft.com/dotnet/core/sdk:3.1.201-buster image, to avoid installing it myself.

The project seems to build fine on Windows (without Docker), but I don't have any Windows machine available. Am I missing something in the .csproj files? Some says that I should install Mono, but I've tried to install mono-complete package and still got the same error.

Does someone have any idea (it may be just a small mistake, since I don't know .NET environment very well) ?

Edit : build fails with .NET Core image, but succeed with mono

I forgot to mention it but the build inside a Docker container works with the base image mono:6.8.0.96 and the following Dockerfile :

FROM    mono:6.8.0.96

WORKDIR /app

# copy all files necessary to resolve dependencies
COPY    MyProject.sln .
COPY    MyProject/MyProject.csproj MyProject/
COPY    MyProjectUnitTests/MyProjectUnitTests.csproj MyProjectUnitTests/

# resolve dependencies
RUN     nuget restore

# copy all files
COPY    . .

RUN     msbuild

As @LexLi pointed out in a comment, some code needs to be refactored (notably WPF API calls because Mono doesn't support it), but the build succeeds with Mono.

To avoid this refactoring, I'd like to use the Microsoft base images instead of Mono if possible.

4
  • You have a .NET Framework app but you're trying to build it in a .NET Core container? That's just not going to work. You need to use a container that contains the proper tools for building .NET Framework apps, or switch your app to target .NET Core. Commented Apr 18, 2020 at 23:31
  • 1) With Mono installed, you should call msbuild to compile the projects. However, WinForms on Mono is never a completed attempt, so you shouldn't expect it works all the time. 2) WinForms on .NET Framework or .NET Core is Windows only, so like the others commented, you can only use a Windows machine (or Windows Docker image) to compile it, with the right tooling (msbuild or dotnet CLI). Commented Apr 19, 2020 at 0:45
  • @mason Thank you for your answer. I'm not an expert in .NET environments, sorry. Could you please tell me more about "switch your app to target .NET Core"? I can't use .NET Framework because the Docker base image is not available on GNU/Linux hosts. Commented Apr 19, 2020 at 8:44
  • @LexLi Thank you for your answer. I added a Dockerfile working based on mono image and a little refactoring to make the build pass. AFAIK, .NET Core is not Windows only, check learn.microsoft.com/en-us/dotnet/core/install/… for example. Commented Apr 19, 2020 at 8:45

2 Answers 2

5

TLDR: You cant build a Desktop app for Linux by using only the .NET stuff from Microsoft. Your alternatives are Mono or a third party UI stack.

You cannot build a WinForms application on Linux with .NET Framework or .NET Core. To uderstand this you must understand the .NET ecosystem:

Runtimes

  • .NET Framework is Windows only, and it's done. Do not expect much change on .NET Framework. To build you application on .NET Framework you are forced to use Windows

  • .NET Core is the fairly new cross platform .NET that can run almost on everywhere. While .NET Core is cross-platform, WPF and WinForms still are Windows only.

  • Mono is an alternative open-source implementation of .NET Framework, Mono includes a WinForms implementation but as @Frank Alvaro said already YMMV.

UI Frameworks

.NET Core supports building Desktop Applications since version 3.0 but only on Windows. Both UI Frameworks, WinForms (Windows Forms) and WPF (Windows Presentaion Foundation) are more or less wrappers around Windows API and currently Microsoft does not seem to be interested in making them cross-platform. So sadly no official WinForms or WPF for Linux.

There are some third party alternatives for .NET Core for example: Avalonia or GtkSharp.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer and clarification. I understand better now. To resume (if I really have understood your answer), I must use Mono here to build my app on GNU/LInux in a Docker container. .NET Core images are not suitable here because WinForms are still Windows only. Am I right?
Yes you are right. Being I just tried to build a .net core winforms app in a Linux container, and the result is: NETSDK1100: Windows is required to build Windows desktop applications. So you can't even build for windows in docker :(
Thanks, so let's go using Mono then. It's weird because on another question, someone told me (stackoverflow.com/questions/61278260/…) that porting C# apps to Mono instead of .NET Core is kind of an "heresy". What do you think about that?
I don't thik mono is heresy, the xamarin stuff also relies on mono. Without UI I would tend to .net core. Generelly spoken I would suggest to port all non-UI code to .NET Standard class libraries. This way you can consume the libraries everywhere and defer the framework decision to the application and keep all options.
2

As @mason indicated, you'll need to pull an image that can use the .NET Framework SDK. Grab the official image for the .NET Framework SDK, which will allow you to dotnet restore/build...

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8

WORKDIR /app

# copy all files necessary to resolve dependencies
COPY    MyProject.sln .
COPY    MyProject/MyProject.csproj MyProject/
COPY    MyProjectUnitTests/MyProjectUnitTests.csproj MyProjectUnitTests/

# resolve dependencies
RUN     dotnet restore --verbosity normal

# copy all files
COPY    . .

# Build the app
WORKDIR /app/MyProject
RUN     dotnet build

# Uncomment to run the tests (assuming MyProjectUnitTests is a test project :-)
#WORKDIR /app/MyProjectUnitTests
#dotnet test

There's a good sample of a .NET Framework Dockerfile here. YMMV with this on Mono

1 Comment

Thanks for your answer. Unfortunately, mcr.microsoft.com/dotnet/framework/sdk:4.8 image is not available on Linux machines (docker pull returns an error : no matching manifest for linux/amd64 in the manifest list entries), and so requires a Windows host (that I don't have). Indeed, I'm trying to build the project under a GNU/LInux host, like I said at the beginning of my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.