App opens console window when being build with Docker - c#

I'm facing the issue that a .Net Core WPF application automatically opens a console window when started. This only happens when build inside a Docker container. When I build it directly on my PC, only the actual application window opens.
My best guess is that this is an issue with the operating system the .Net Core image is based on. The .Net Core SDK Docker Hub Repo knows the following tags: 3.1-nanoserver-1809, 3.1-nanoserver-1903, 3.1-nanoserver-1909, 3.1-nanoserver-2004, 3.1-nanoserver-2009. I was able to confirm the issue with the first three tags, but the 2004 and 2009 tags do not run on my machine, so I need someone to try this out and either confirm my theory (which would mean that it should not happen on at least on of these images) or to come up with a better explanation of why this is happening.
This is reproducible with the default .Net Core WPF app Visual Studio creates for you. Here is a Dockerfile to test it out:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . ./
RUN dotnet build -c Debug -o out
FROM stefanscherer/chocolatey
WORKDIR /app
RUN choco install -y 7zip
# Depending on your project setup it might be src/[project name]/out
COPY --from=build /src/out ./test
RUN 7z a -y test.zip ./test/*
You can build the image and extract the compiled program with the following commands:
docker build -t testimage .
docker run -d --name testcontainer testimage
docker stop testcontainer
docker cp testcontainer:app/test.zip .

I was able to reproduce this problem on both 3.1-nanoserver-2009 and 3.1-nanoserver-2004 for you.
I think the problem is related to the warning printed out during build:
warning NETSDK1074: The application host executable will not be customized because adding resources requires that the build be performed on Windows (excluding Nano Server).
If that's the case, then it seems that it is a limitation of the nanoserver base image, and unfortunately it looks like this problem still has not been resolved, because it's still present when building in mcr.microsoft.com/dotnet/nightly/sdk:5.0.
Here's a related pull request that might shed some light on the subject.
Having said that, I think the only option for now is to use windows image other than nanoserver (alternatives can be found here). I didn't find any image that would come with .NET Core SDK preinstalled (I didn't put much effort into finding it though), but it should be fairly simple to set it up. In the following example I used servercore image since it is much more lightweight than windows image.
FROM mcr.microsoft.com/windows/servercore:20H2 AS sdk
WORKDIR /dotnet
# Download the official .NET Core install script
RUN powershell -c "Invoke-WebRequest -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1"
# Run the install script
RUN powershell -c "& ./dotnet-install.ps1 -InstallDir ."
# Add the installed executable to PATH
RUN setx PATH "%PATH%;/dotnet"
FROM sdk AS build
# Do your stuff here
Here you'll find the documentation for the install script.
I also did confirm that the produced application did not spawn console window when run.

As #Grx70 mentioned, the underlying nanoserver image is the problem.
There is another .NET core image, but it is based on the servercore image.
This saves the manual installation of .NET Core within the image and has everything ready at hand.
Here you can find all version of the .NET Core Image:
DockerHub
Way down at the bottom, you'll find the heading Windows Server Core 2019 amd64 Tags and the associated mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 image.
This image no longer produced a console window when I built something :)

Related

Selenium tests not running on Docker using C#

I am trying to run my selenium tests in Docker but I don't see any containers running and don't see anything in the docker logs.
I have created 3 different versions of DockerFiles which can be found in https://github.com/devanasri/googlesearchauto
The commands I am using to create image and start container is
docker build -t googlesearchauto .
docker run -it automation
When run above commands to start the container and run selenium tests, nothing happens and sometimes I get below error when trying to start the container.
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'test' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
Download a .NET SDK:
https://aka.ms/dotnet-download
Learn about SDK resolution:
https://aka.ms/dotnet/sdk-not-found
I tried to run each version of the DockerFile but all of them not working. Any help is appreciated.

Build ASP.Net Core Deploy Package on Linux

I am trying to build a web deploy package on the .net core sdk docker image to be used to deploy to a windows server running iis (the deployment being done on a seperate windows vm). This is the command I am running (this can also be done via the publish UI in visual studio):
dotnet publish src/SpaceForce.sln -c release -o out -p:WebPublishMethod=Package;PackageAsSingleFile=True;DesktopBuildPackageLocation="../Package/SpaceForce.WebAPI.zip"
On Windows this produces a folder with the following files and then when i'm ready to deploy, i just run the cmd file:
SpaceForce.WebAPI.deploy.cmd
SpaceForce.WebAPI.deploy-readme.txt
SpaceForce.WebAPI.Parameters.xml
SpaceForce.WebAPI.SetParameters.xml
SpaceForce.WebAPI.SourceManifest.xml
SpaceForce.WebAPI.zip
but on Linux (.net core sdk docker image) it produces the following error:
/usr/share/dotnet/sdk/3.1.300/Sdks/Microsoft.NET.Sdk.Publish/targets/PublishTargets/Microsoft.NET.Sdk.Publish.MSDeployPackage.targets(97,5): error MSB6004: The specified task executable location "%ProgramW6432%/IIS/Microsoft Web Deploy V3/msdeploy.exe" is invalid. [/home/osboxes/src/spaceforce/src/SpaceForce.WebAPI/SpaceForce.WebAPI.csproj]
on this page, it has this note but i'm not sure if this applies in my case:
The dotnet msbuild command is a cross-platform command and can compile ASP.NET Core apps on macOS and Linux. However, MSBuild on macOS and Linux isn't capable of deploying an app to Azure or other MSDeploy endpoints.
If i can build on linux, i am happy to run the cmd file on a windows box for deployment. Is it possible to build this package on linux?
As you quoted, MSDeploy is a Windows-only thing. Using the WebPublishMethod switch tries to run "%ProgramW6432%/IIS/Microsoft Web Deploy V3/msdeploy.exe", which is obviously not present in the linux container.
To publish an app from a linux environment to an IIS server you have to publish the app without the WebDeploy specific features and then send the published files to the IIS server. On the server you configure an app with a set physical path and move the published files into that path.
To get the files from the docker image you have to create a temporary container and copy the files
# Dockerfile
RUN dotnet publish src/SpaceForce.sln -c Release -f netcoreapp3.1 -o /out
# bash script to get the publish dir from the image
imageId=$(docker create <IMAGE_NAME>)
docker cp $imageId:/out/ <HOST_DIR>
docker rm $imageId
This article should get you started.
Hope this helps a litte 🙂

How to publish my WPF solution with Rider?

I cannot seem to deploy my WPF app using the Rider IDE. Most support tell me to use the "Publish" button in VS which of course doesn't really help me.
I do produce an executable with the .NET Project and .NET Executable build Configurations, but it doesn't seem to run on any other machine, where it seems to close immediately without spitting error messages (even from PowerShell)
N.B. the app launches fine from my own machine, and from anywhere I choose to move it to.
I've tried to set Edit Configurations... > Runtime arguments: to
dotnet publish -c Release -r win10-x64
as suggested in this post, but that wasn't enough.
Running dotnet publish -c Release -r win10-x64 .\my_app.csproj from PowerShell returns
error MSB4216: Impossible to execute the task "GenerateResource"
[...]
Failed to connect to "CLR4" runtime and the "x86" architecture.
Make sure that
(1) The necessary runtime and/or architecture is present on the machine
(2) "C:\Program Files\dotnet\sdk\<version>\MSBuild.exe" exists and
has permissions to execute.
Now MSBuild is missing from that folder, but there is MSBuild.dll there.
Am I really missing a fool-proof easy way of publishing a C# WPF solution with Rider?
I use https://wixtoolset.org/ to publish my apps. It's free and independent of the IDE...

How do I build an exe in VS 17 Mac to run in Windows

I wrote a console app in C# on VS 2017 for mac, and the resulting build file is a .dll. I can run that fine in the IDE and from the terminal with "dotnet blah.dll".
What I really want is to give my little console app to my Windows friends who can then run it.
I'm not finding detail on the web or here on how to tell VS2017 mac to make me a Windows exe.
There is a framework-dependent deployment assuming your friends have .net core runtime, they can also run with "dotnet blah.dll"
Otherwise you need to configure self-contained deployment using dotnet publish -c release -r win10-x64 command and some project configuration. More details are available at dotnet publish MSDN article and deployment strategies overall
You need to have a runtime identifier in your csproj for Windows in order for a Windows-compatible binary to be generated. Similar for various Linux distributions.
A list of IDs can be found at: https://learn.microsoft.com/en-us/dotnet/core/rid-catalog. For example, to target Windows 10 x64 you would use win10-x64.
Once you have the csproj configured and have rebuilt you should see a win10-x64 folder in your build output that will contain the files you need.

Using dotnet from docker to power Visual Studio C# extension (OmniSharp)

I have recently tried to do some C# development on my Linux box with the help of the docker image microsoft/dotnet which works great within the docker image.
Yet I would like to use Visual Studio Code with the C# extension powered by OmniSharp to get intellisense/autocompletion, and the other benefits of that extension within that editor.
The problem is that .NET Core is not supported on my distribution (Arch). There is an AUR package but it has been more often broken than working and I would rather stick with my docker image to run dotnet in a container.
Hence my question is: can I make Visual Studio Code's C# extension which is running on my host OS use the dotnet tools available in the docker image?
Thanks
Yep, you can hook your C# code in the container via Docker Volumes. Try something like this:
docker run --name app -d -p 5000:5000 -v <your-project-path>:/app microsoft/dotnet
Note: Make sure that you are using the .NET Core SDK image.

Categories