Heroku Push rejected, failed to compile ASP.NET Core app - asp.net

I exactly followed this link http://codersblock.com/blog/how-to-run-net-on-heroku/
to deploy the application on heroku but I am getting an error
Push rejected, failed to compile ASP.NET Core app.
when I execute git push heroku master.
If any body knows about the issue, it will be great help.
Thanks.

what is heroku-16?
there are different types of distros(stacks) which contains your app:
heroku-16
cedar-14 (supports dotNet-core.)
thats why we will switch to cedar-14
how to fix:
1- install heroku cli. (https://devcenter.heroku.com/articles/heroku-cli)
2- open cmd. type:
cd to/project/Root
heroku stack:set cedar-14
git push heroku master

You could create a Dockerfile to generate a Docker image runnable on Heroku.
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <YOUR_API_NAME>.dll
Then you can publish it to Heroku either directly using these Docker commands:
docker build -t aspnetapp <YOUR_API_NAME>
docker login --username=$HEROKU_USERNAME --password=$HEROKU_API_KEY registry.heroku.com
docker tag aspnetapp registry.heroku.com/$HEROKU_APP_NAME/web
docker push registry.heroku.com/$HEROKU_APP_NAME/web
In order these commands:
Create a Docker image and builds a release of your project in it
Connect to Heroku's docker registry using your credentials
Creates a new tag for your image
Publishes your image to your Heroku app
However, if your machine does not support Docker, you can also use CircleCI to run these commands. More details here:
https://www.codingnagger.com/2018/02/21/continuous-delivery/

I've a similar problem and spent almost the whole day debugging it.
My issue, I was using Microsoft.Net.Compilers and I'd to remove that
<PackageReference Include="Microsoft.Net.Compilers" Version="4.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
and add
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
To .csproj (My case I was using clean architecture so I was doing these modifications in Domain layer
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
Note, I also have upgraded some of the packages it was complaining about been downgraded.

Related

.Net Dockerizing excluding build stage in the dockerfile

I am new to docker and AKS, I am working on a .Net project that is migrated from .Net core 3.1 to .Net 6 , I am trying to containerize and deploy to AKS. First i wanted to test in the local machine so i had setup docker in the macbook m1 and followed this link https://learn.microsoft.com/en-us/visualstudio/mac/docker-quickstart?view=vsmac-2022 to create a dockerfile, I am able to run the dockerfile from Visual Studio if i follow the steps given in the link but if i run docker compose command from terminal i am getting issues with SDK version, however what i am trying to achieve is remove the build stage and directly run the .net application as the build stage is taken care by Jenkins and the DLL is placed in a folder
Here is the complete dockerfile with all the stages
FROM mcr.microsoft.com/dotnet/core/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:6.0 AS build
WORKDIR /src
COPY DockerDemo/DockerDemo.csproj DockerDemo/
RUN dotnet restore "DockerDemo/DockerDemo.csproj"
COPY . .
WORKDIR "/src/DockerDemo"
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]
I modified dockerfile excluding build stage, I am taking DLL file from a "lib/net6.0" that is built by jenkins and placed in this folder
FROM mcr.microsoft.com/dotnet/core/aspnet:6.0 AS base
COPY ["lib/net6.0","/src/"]
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM base as final
WORKDIR /app
COPY --from=base /src/ .
ENTRYPOINT["dotnet","DockerDemo.dll"]
If i build the dockerfile the image is getting created but when i run I am getting following error
unhandled exception system.io.filenotfoundexception could not load
file or assembly 'NLog, Version=.5.0.0.0
The project is using NLog.Web.ASPNetCore 5.1.4 version, i tried downgrading it to 5.0.0 but i am still getting the same error, i also tried adding just NLog 5.0.0 package but no luck
As i said i am new to Docker and also to the .net development on macbook
My Questions
Q1) Is it possible to exclude the build stage in dockerfile and directly run
Q2) Does the macbook m1 completely supports .Net Core and .Net 6 development so that it can be deployed to AKS via Jenkins pipeline

Deploy Blazor Wasm with Docker from Visual Studio

I want to deploy my Blazor Webassambly App to a Docker Container to run it on my synology NAS.
So my problem is now, that since I want to run my Webassembly App in my VisualStudio IDE with Docker, I have an error "The library 'hostpolicy.dll' required to execute the application was not found in ..."
Has anyone an idea what i could do do solve this error?
Here is the full console output:
docker exec -i -e ASPNETCORE_HTTPS_PORT="55427" -w "C:\app" d274a895523ac47657707b86a7df5ea304df8bfe68330a64b00d9cc357ed1546 "C:\Program Files\dotnet\dotnet.exe" --additionalProbingPath c:\\.nuget\\fallbackpackages2 --additionalProbingPath c:\\.nuget\\fallbackpackages "C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.dll"
Cannot use file stream for [C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.deps.json]: No such file or directory
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app\bin\Debug\net5.0\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' file and specify the appropriate framework.
docker exec -i -e ASPNETCORE_HTTPS_PORT="55427" -w "C:\app" d274a895523ac47657707b86a7df5ea304df8bfe68330a64b00d9cc357ed1546 "C:\Program Files\dotnet\dotnet.exe" --additionalProbingPath c:\\.nuget\\fallbackpackages2 --additionalProbingPath c:\\.nuget\\fallbackpackages "C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.dll"
Cannot use file stream for [C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.deps.json]: No such file or directory
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app\bin\Debug\net5.0\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' file and specify the appropriate framework.
And here is my Docker File:
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Jordan_Webapp_Client/Jordan_Webapp_Client.csproj", "Jordan_Webapp_Client/"]
RUN dotnet restore "Jordan_Webapp_Client/Jordan_Webapp_Client.csproj"
COPY . .
WORKDIR "/src/Jordan_Webapp_Client"
RUN dotnet build "Jordan_Webapp_Client.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Jordan_Webapp_Client.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Jordan_Webapp_Client.dll"] ```
If you're using a private nuget feed, try changing the "RUN dotnet restore..." command.
Example:
RUN dotnet restore -s "https://your-nuget-feed.example.com/nuget" -s "https://api.nuget.org/v3/index.json" "Jordan_Webapp_Client/Jordan_Webapp_Client.csproj"

Errors installing microsoft-net-sdk-blazorwebassembly-aot in Dockerfile

I'm trying to run dotnet workload install microsoft-net-sdk-blazorwebassembly-aot from my Dockerfile while building my Blazor project in order to enable AOT WASM compilation.
Here is the relevant Dockerfile code:
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal-amd64 AS builder
...
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot
RUN dotnet publish -c Release
However, this fails with an error like:
Updated advertising manifest microsoft.net.sdk.ios.
Updated advertising manifest microsoft.net.sdk.maccatalyst.
Updated advertising manifest microsoft.net.sdk.macos.
Updated advertising manifest microsoft.net.workload.mono.toolchain.
Updated advertising manifest microsoft.net.sdk.tvos.
Updated advertising manifest microsoft.net.sdk.android.
Installing workload manifest microsoft.net.sdk.ios version 14.5.100-preview.5.894.
Workload installation failed, rolling back installed packs...
Installing workload manifest microsoft.net.sdk.ios version 14.5.100-preview.5.881.
Installation roll back failed: Failed to install manifest microsoft.net.sdk.ios version 14.5.100-preview.5.881: The transaction has aborted..
Workload installation failed: Failed to install manifest microsoft.net.sdk.ios version 14.5.100-preview.5.894: Invalid cross-device link.
I also tried with --skip-manifest, and this appears to install the workload, but then fails to perform the AOT build.
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot --skip-manifest-update
Installing pack Microsoft.NETCore.App.Runtime.Mono.browser-wasm version 6.0.0-preview.5.21301.5...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.browser-wasm version 6.0.0-preview.5.21301.5...
...
Garbage collecting for SDK feature bands 6.0.100...
Successfully installed workload(s) microsoft-net-sdk-blazorwebassembly-aot.
RUN dotnet publish -c Release
...
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
AOT'ing 50 assemblies
/usr/share/dotnet/packs/Microsoft.NET.Runtime.WebAssembly.Sdk/6.0.0-preview.5.21301.5/Sdk/WasmApp.targets(507,5): error MSB3073: The command "emcc --version" exited with code 1.
Any advice?
Morgan, you're hitting two issues:
On the skip-manifest (which is the workaround) is issue https://github.com/dotnet/sdk/issues/18450
The emcc --version error is issue https://github.com/dotnet/runtime/issues/54342 and resolved by installing Python first...see example: https://github.com/timheuer/PictureFixer/blob/docker/PictureFixer/Dockerfile#L11-L12 (on docker branch in the repo)
You will then hit a different bug (could not find cross compiler) which is issue https://github.com/dotnet/runtime/pull/54651 (which has been checked in).
Once #3 is fixed in an image you can consume, it should work ensuring you add #2 in your script. Example full Docker file:
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
#FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS build
COPY ["nuget.config", "nuget.config"]
RUN apt-get update
RUN apt-get install --no-install-recommends --yes python3
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot --skip-manifest-update
WORKDIR /src
COPY ["Server/PictureFixer.Server.csproj", "Server/"]
COPY ["Shared/PictureFixer.Shared.csproj", "Shared/"]
COPY ["Client/PictureFixer.Client.csproj", "Client/"]
RUN dotnet restore "Server/PictureFixer.Server.csproj"
COPY . .
WORKDIR "/src/Server"
RUN dotnet build "PictureFixer.Server.csproj" -c Release -o /app/build --nologo
FROM build AS publish
RUN dotnet publish "PictureFixer.Server.csproj" -c Release -o /app/publish --nologo
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PictureFixer.Server.dll"]
You can temporarily work around #3 by adding this to your Blazor Wasm project csproj:
<PropertyGroup>
<_MonoAotCrossCompilerPath>#(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
</PropertyGroup>

Cannot connect to azure nuget feed when creating a container

I have been trying to get docker to pull from a private nuget feed which is hosted within Azure Devops, but I consistently get a 401 unauthorized error. I have looked a numerous other answers and resources, but all of which have not helped. My dockerfile script is the following
# escape=`
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /src
EXPOSE 80
EXPOSE 443
ARG FEED_URL
ARG FEED_ACCESSTOKEN
# Install powershell via dotnet
RUN dotnet tool install --global PowerShell --version 7.0.0
SHELL ["pwsh", "-command"]
## CURRENT ERROR - "-AddNetfx" - NOT CONSIDERED AN ARGUMENT, ALTHOUGH IT IS (CAN BE SEEN IN THE SCRIPT ENTRY) ##
# Install credprovider which is required for accessing private artifact repository
RUN Invoke-WebRequest https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1 -OutFile installcredprovider.ps1; `
.\installcredprovider.ps1 -AddNetfx; `
del installcredprovider.ps1
#################################################################################################################
# Copy csproj and sln files
COPY ["Abstractions/*.csproj", "./Abstractions/"]
COPY ["AdminServicesPortal/*.csproj", "./AdminServicesPortal/"]
COPY ["DataInterface/*.csproj", "./DataInterface/"]
COPY ["Resources/*.csproj", "./Resources/"]
# Need to copy over docker NuGet packages
COPY "AdminServicesPortal/nuget.config" "AdminServicesPortal/"
# Enable session token cache
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS `
"{`"endpointCredentials`": [{`"endpoint`":`"https://pkgs.dev.azure.com/ORG/PROJECT/_packaging/FEED/nuget/v3/index.json`", `"username`":`"USER`", `"password`":`"PAT`"}]}"
RUN echo $Env:VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
# Restore dependencies before copying everything else over
RUN dotnet restore "AdminServicesPortal/Portal.csproj"
# Copy the rest of the files required for a build
# Note that this is separate, because we should stop building
# the container if we do not have all required dependencies
COPY Abstractions/. ./Abstractions/
COPY AdminServicesPortal/. ./AdminServicesPortal/
COPY DataInterface/. ./DataInterface/
COPY Resources/. ./Resources/
WORKDIR "/src/AdminServicesPortal"
RUN dotnet build "Portal.csproj" -c Release -o /app/build
# Publish application
FROM build-env AS publish
RUN dotnet publish "Portal.csproj" -c Release -o /app/publish
# Runtime environment for container
FROM build-env AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MPX.AdminServices.Portal.dll"]
The error I get from Visual Studio is the following
2> Failed to restore C:\src\Resources\Resources.csproj (in 2.99 sec).
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/{ORG}/{PROJECT}/_packaging/{FEED}/nuget/v3/index.json. [C:\src\AdminServicesPortal\Portal.csproj]
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Response status code does not indicate success: 401 (Unauthorized). [C:\src\AdminServicesPortal\Portal.csproj]
From what I can tell in the link below; my dockerfile script should work
https://github.com/dotnet/dotnet-docker/blob/master/documentation/scenarios/nuget-credentials.md#using-the-azure-artifact-credential-provider
To ensure it wasn't an issue with the PAT of the user not having access, I gave them access to everything across the whole entire organisation. Any help would be greatly appreciated!
Note #1: My current suspicion is that there are some additional parameters I might need to pass, since the private nuget feed is for a project rather than the entire organisation?
Note #2: I ran the following PowerShell command (https://learn.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops#package-and-publish-the-module) which worked fine, so I am even more confused to why my dockerfile script does not work.
nuget sources Add -Name "PowershellModules" -Source "https://pkgs.dev.azure.com/<org_name>/_packaging/<feed_name>/nuget/v3/index.json" -username "<user_name>" -password "<personal_access_token(PAT)>"
You can check out below workarounds without using NuGet credential plugin.
1, Use dotnet cli to add credentials to the nuget source in your docker file. And then pass the $PAT in the build arguments (ie. --build-arg PAT=$PAT ):
ARG PAT
COPY . .
RUN dotnet nuget add source "your-source-url" --name "source-name" --username "useless" --password "$PAT" --store-password-in-clear-text
RUN dotnet restore
2, You can also try adding the credentials to the config file using the nuget command. And include the nuget.config which has the credentials in the build context. And copy the nuget.config in your docker file . See below:
sources Add -Name "MyPackages" -Source "https://my.pkgs.visualstudio.com/_packaging/MyPackages/nuget/v3/index.json" -username any -password $PAT -ConfigFile Source/Nuget.config -StorePasswordInClearText
Copy the nuget.config in the docker file, You can delete the nuget.config file when the restore is complete:
COPY *.csproj .
COPY ./nuget.config .
RUN dotnet restore
RUN rm nuget.config
You can refer to my answer to this similar thread that using azure nuget feed in the docker container in Azure pipelines.

Is it possible to run my development asp.net core server out of a docker container

When developing node.js projects I will normally use docker compose to bring up an entire stack of docker containers in order to develop on my local host. I mount volumes in and use nodemon to automatically update the running server. This works well. However, it does not seem to work quite the same way with asp.net core.
Is it possible to run a development container for an asp.net core application? I would like to have the server restart when there is a change on the host machine to a file. I know you can do this with the dotnet watch run command but I'm yet to see it working inside a running docker container. How can I get this working within a docker compose stack? What docker base image should I be using? Ideally this solution will work from the command line without using visual studio as I'm using Visual Studio code on a mac.
A little bit late to the party, but maybe it will help someone.
Here is the sample project setup that will leverage dotnet watch command
in a docker container, while source code is being edited on the host system.
As far as I know dotnet watch now is a global tool so you can use it with
different versions of .netcore.
Here is the documentation for
dotnet watch command.
Sample project overview
Here is a small example project for demo purposes, made with help of minimal api.
It has one endpoint /freshbread which returns "Baked bread".
Folder structure:
Bakery
│ Dockerfile
└───src
│───Bakery.csproj
└───Program.cs
Content of Program.cs:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/freshbread", () => "Baked bread");
app.Run("http://*:80");
Content of Bakery.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Content of Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0
WORKDIR /app
ENTRYPOINT ["dotnet", "watch"]
Based on a Microsoft .net core SDK image. It just defines an /app workdir and launches there dotnet watch command.
The "trick" here is to mount into /app folder your source code from the host machine and let container handle compilation
and server restart, so:
Build an image (from Bakery folder):
docker build -t dev-container .
Run a container:
docker run \
--rm \
--detach \
--volume /path/to/the/Bakery/src:/app \
--publish 5000:80
dev-container
Publish port outside, so api will be accessible from the host machine.
Now, container is watching files in mounted folder and whenever there are chages
it will perform hot-reload.
Demo
Here is a short demo of this setup, note that top and bottom-right splits are host machine terminals and bottom-left split is log of the container.

Resources