简体   繁体   中英

Building a WinForms C# application with Docker .NET Core image results to an error : .NETFramework,Version=v4.6.1 were not found

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.

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 .

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

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