Why can't the dotnet cli 3.x build a docker image? - docker

I've got a build script which was designed for dotnet 2.1. This script's job is to output a docker image that runs a dotnet core app. I recently made a small change to the dotnet core app but when I went to build a container, using my script, the image didn't work. I debugged this down to dotnet 3.1 behaving differently when calling the dotnet publish command...
Here is the command I'm using in dotnet 2.1:
dotnet publish WebService --configuration %CONFIGURATION% --output bin\Docker
This creates a Docker folder in the bin folder, as expected, but when I use dotnet 3.1 this output argument appears to be ignored.
I found this on the following on MSDN:
If you're using an SDK that is newer, like 3.0, make sure that your
app is forced to use the 2.2 SDK. Create a file named global.json in
your working folder and paste in the following JSON code:
{ "sdk": {
"version": "2.2.100" } }
https://learn.microsoft.com/en-us/dotnet/core/docker/build-container
So, after finding this info on MSDN that says that I need to force dotnet to use an older version, I now have a workaround but my question is... why? Dotnet 3.1 can't build Docker images? Please help me connect the dots on what I'm missing here.

Related

The imported project Microsoft.WebApplication.targets was not found error while creating Docker Image using Docker file

I am trying to create Docker Image and while running Run dotnet build xxxxxx.csproj -c Release -o /app/build I am getting below error.
The imported project "C:\Program Files\dotnet\sdk\7.0.101\Microsoft\VisualStudio\v17.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\7.0.101\Microsoft\VisualStudio\v17.0\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk.
Can anyone suggest me the fix?
I got an answer
I was running wrong command to build a project based on ASP.Net framework 4.7.2.
Instead of using RUN DOTNET BUILD, I needed to use RUN msbuild command. It solved my issue. It is working now.

Dotnet Watch Run gives me a Rosetta Error: attachment of code signature supplement failed: 1 after save

I'm having a problem with my .Net Core 3.1 Project. I'm using Docker for hosting the MS SQL Database (image azure-sql-edge) and I run it on a MacBook Pro M1 Max.
When starting the project with Dotnet Watch Run everything works ok but after a save in Visual Studio Dotnet Watch Run restarts and gives me an error:
rosetta error: /var/db/oah/0cbcd548c398ac044cf47633c4e5aa068c1a0416a18ad1861a768ac56fd1d33b/68b61c75aa9514f21db1470814e91bac8c95ea1a32f4e42fc88601dc4eeac1fc/Project.aot: attachment of code signature supplement failed: 1
And Dotnet watch gives a:
dotnet watch ❌ Exited with error code 133
Anybody has a clue what's going wrong here?
I am a frontend developer and I am no expert in dotnet, it just so happened that our backend is dotnet and In my case, running dotnet clean then running my backend again dotnet run --project=MyProject help me solve this issue. Hope this helps you solve your issue.
I got same issue on M1 with .netcore 3.1
tried serval things. but didn't solved. but, yesterday my co-worker said try run dotnet clean and It works like magic.
since before I have to reboot my pc everytime when this error occured. and now I am happy. hope this simple command helps.
once again, just run dotnet clean no other flags used.
I had the same issue and resolved it by updating the target framework to the newest version on my machine - typing "dotnet --version" into the command line will show you the latest version you are running.
For me it was going into the .csproj file and updating the target framework within there. Hope that helps
After updating Dotnet DSK to 3.1.425 (release date November 8th 2022) the issue was fixed.
I had the same issue on M1 with .Net 6.0.402. Upgrading MacOS to Ventura 13.0 (22A380) solved it for me.
Updating to latest SDK 7.0.100-rc.2 did not help here.

Blazor server - localization dlls (xxx.resources.dll) missing inside the output folder when run dotnet publish --configuration release

I have a blazor server application with 2 localization resource files with following names
App.no.resx and App.resx, but these were missing when do the dotnet publish with below command,
dotnet publish --configuration release
but the resource dlls are generated inside "no" folder when run the build from VS2019
Note - Both localization resource files are set as 'embedded resource' and 'do not copy'
can someone please advise me how can I resolve this issue ?

.net core build error when building docker image missing Newtonsoft.Json

I'm trying to build my .net core project onto a docker container but it seems like it can't find Newtonsoft.Json which I thought was included in the asp.net core
Here is my docker file:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 58373
EXPOSE 44370
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TestCode/TestCode.csproj", "TestCode/"]
RUN dotnet restore "TestCode/TestCode.csproj"
COPY . .
WORKDIR "/src/TestCode"
RUN dotnet build "TestCode.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TestCode.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestCode.dll"]
I'm getting the following error when trying to build
Error Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
In my case, When used JsonProperty in a class,
Visual Studio intellicode auto-filled,
using Newtonsoft.Json;
Then during docker build,
warning MSB3245: Could not resolve this reference. Could not locate the assembly "Newtonsoft.Json". Check to make sure the assembly exists on disk.
When I checked, found out that the Visual Studio added an Assembly Reference to Newtonsoft.Json (you can find this by expanding Dependencies node in the solution explorer in Visual Studio). And I was using Linux images.
So in order to solve this, I removed the Assembly Reference, and added nuget package Newtonsoft.Json, then the docker build was successful.
Found the issue. Update NuGet package Microsoft.VisualStudio.Azure.Containers.Tools.Targets to latest version and the problem goes away.
I Don't know why, but I hit this issue because of my tests projects. Once I removed my tests from the release configuration (meaning "I Don't want the test assemblies in my publish folder"), the docker file worked great. xUnit using coverlet for code coverage in net core 3.1, might help someone someday, I almost lose it.
EDIT:
Doing further research I found this is an issue in the coverlet package to track code coverage. Can be fixed using <IsPublishable>false</IsPublishable> as Billy Braga mention here https://github.com/coverlet-coverage/coverlet/issues/1045
1 way is to have your other project be part of your solution and reference it as a project reference.
or
2nd way, as a quick resolution what I did and it worked for me, is after COPY . . is to add another copy command in the docker file like:
COPY ./bin/Debug/.../your-referenced-dll-name.dll .
--Hope this helps

donet pack in VS Team Services

I have a problem with my build configuration in VS Team Services.
I try to mark my nuget packages with prerelease suffix.
I have a Utility CommandLine step defined.
Tool: dotnet
Argument: pack $(build.sourcesdirectory)/..../project.json --no-build --configuration Release --output nupkgs --version-suffix "prerelease-$(rev:.rr)"
I get the message:
"'1.0.0-prerelease-$(rev:.rr)' is not a valid version string."
If I do the same with a static:
Tool: dotnet
Argument: pack $(build.sourcesdirectory)/..../project.json --no-build --configuration Release --output nupkgs --version-suffix "prerelease-123"
That works fine.
So, it seams that the special placeholder $(rev:.rr) is not replaced, but I don't know why.
Kind regards
I found out that there are different variables for the build and release tabs.
I tried to use this token on a build definition, what didn't work.
I was able to achive what I want to do with a build token.
Kind regards

Resources