I have a dotnet core 2.2 api application. There are 3 layers: API layer, Business Service and Data access layer. In Data Access Layer I have used IHttpClientFactory.
While running through docker I am getting below errors:
warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.Extensions.Http". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
error CS0246: The type or namespace name 'IHttpClientFactory' could not be found (are you missing a using directive or an assembly reference?) [/app/MyApp.Service/MyApp.DB.csproj]
Here is my docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
COPY *.sln .
COPY MyApp/. ./MyApp/
COPY MyApp.Application/. ./MyApp.Application/
COPY MyApp.DB/. ./MyApp.DB/
RUN dotnet restore
COPY MyApp/. ./MyApp/
WORKDIR /app/MyApp
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/MyApp/out ./
ENTRYPOINT ["dotnet", "MyApp.dll"]
Related
I am trying to build my .Net 6.0 API Project in Visual studio 2022, and I get the following error.
MSB4018 The "ContainerBuildAndLaunch" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' user declined directory sharing
Even though project target OS Framework - Linux, it is looking out for Microsoft.Win32.Registry for some reason. Any direction will be highly appreciated.
Docker file
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["BPBComputerGadgets.ProductsAPI/BPBComputerGadgets.ProductsAPI.csproj", "BPBComputerGadgets.ProductsAPI/"]
RUN dotnet restore "BPBComputerGadgets.ProductsAPI/BPBComputerGadgets.ProductsAPI.csproj"
COPY . .
WORKDIR "/src/BPBComputerGadgets.ProductsAPI"
RUN dotnet build "BPBComputerGadgets.ProductsAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BPBComputerGadgets.ProductsAPI.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BPBComputerGadgets.ProductsAPI.dll"]
The issue was the user declined directory sharing. Opened the docker desktop->settings->Resources->File Sharing and Add the folders -> Apply and restart.
need a little help resolving this issue I'm facing when building my ASP.NET project that references a local class library inside a docker container.
I'm using:
ASP.NET 6.0
Docker version 20.10.21, build baeda1f
dotnet sdk 6.0
When I try to run
docker build .
I get the following error:
=> ERROR [build 7/7] RUN dotnet build "ProjectName.csproj" -c Debug -o /app/build 1.9s
------
> [build 7/7] RUN dotnet build "ProjectName.csproj" -c Debug -o /app/build:
#15 0.434 MSBuild version 17.3.2+561848881 for .NET
#15 0.655 Determining projects to restore...
#15 0.885 All projects are up-to-date for restore.
#15 1.042 /usr/share/dotnet/sdk/6.0.404/Microsoft.Common.CurrentVersion.targets(2302,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "PackageName". Check to make sure the assembly exists on disk. If this reference is required
by your code, you may get compilation errors. [/src/ProjectName/ProjectName.csproj]
#15 1.847 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/ProjectName/ProjectName.csproj]
#15 1.852
#15 1.852 Build FAILED.
#15 1.852
#15 1.852 /usr/share/dotnet/sdk/6.0.404/Microsoft.Common.CurrentVersion.targets(2302,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "PackageName". Check to make sure the assembly exists on disk. If this reference is required
by your code, you may get compilation errors. [/src/ProjectName/ProjectName.csproj]
#15 1.852 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/ProjectName/ProjectName.csproj]
#15 1.852 1 Warning(s)
#15 1.852 1 Error(s)
#15 1.852
#15 1.852 Time Elapsed 00:00:01.38
------
executor failed running [/bin/sh -c dotnet build "ProjectName.csproj" -c Debug -o /app/build]: exit code: 1
The dockerfile I'm using
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ProjectName.csproj", "ProjectName/"]
RUN dotnet restore "ProjectName/ProjectName.csproj"
COPY .. .
WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Publish -o /app/build
FROM build AS debug
RUN dotnet publish "ProjectName.csproj" -c Publish -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]
The .csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
....
</ItemGroup>
<ItemGroup>
<Reference Include="PackageName">
<HintPath>..\..\PackageName\PackageName\bin\Debug\netstandard2.1\PackageName.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Any thoughts on me getting the dockerfile to fetch the package .dll on build? or is it the wrong way to go about getting what I want?
I am open to hosting the package on nuget but for the time being this dockerfile is for dev purposes only.
Keep in mind that the package is at the right location locally, as building the project using:
dotnet build ProjectName.csproj
works fine
P.S. I replaced the package name with "PackageName" and the project name with "ProjectName".
I tried building the dockerfile for ASP.NET 6.0 and I expected it to pass, but I got an Error instead.
So I solved this almost immediately, I made the class library build to the location with my asp.net project, cleared the docker cache, and built again.
Id project located on your computer, You need to copy xxx.dll after define
SWORKDIR /src
COPY ["Xxx.csproj", "."]
COPY ["output/Debug/net6.0/xxx.dll", "."]
Otherwise you have to create nuget package the dll then you install from nuget
I am trying to run my first .Net Core Web API(Core 2.2) on Docker in local host. I enabled Docker support(Linux) while creating the project.
Docker File
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SampleApp/SampleApp.csproj", "SampleApp/"]
RUN dotnet restore "SampleApp/SampleApp.csproj"
COPY . .
WORKDIR "/src/SampleApp"
RUN dotnet build "SampleApp.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "SampleApp.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SampleApp.dll"]
When I try to run the API on Docker from VS, I get the following error
Error CTP1001 An error occurred while attempting to build Docker image.
and when I looked into the output window
1>------ Build started: Project: SampleApp, Configuration: Debug Any CPU ------
1>SampleApp -> E:\Sandbox\ContainerDeployment\SampleApp\SampleApp\bin\Debug\netcoreapp2.2\SampleApp.dll
1>docker build -f "E:\Sandbox\ContainerDeployment\SampleApp\SampleApp\Dockerfile" -t sampleapp:dev --target base --label "com.microsoft.created-by=visual-studio" "E:\Sandbox\ContainerDeployment\SampleApp"
1>#1 [internal] load build definition from Dockerfile
1>#1 transferring dockerfile: 32B done
1>#2 [internal] load .dockerignore
1>#2 sha256:0237a52bf01ebc117c468d73e0fc890d42166c17898aa5c4fd0fd96dcaee67e8
1>#1 sha256:bb73acd705ce58a2a5ccfdd8d06dc6e46e04a03a430562efdf99961870d2e178
1>#2 transferring context: 34B done
1>
1>#2 DONE 0.0s
1>
1>#3 sha256:2209054a124f7bc1424fa6735d588f36916bcec272f45a5e3e54f7288e65d73e
1>#3 [internal] load metadata for docker.io/microsoft/dotnet:2.2-aspnetcore-runtime
1>#1 DONE 0.0s
1>#3 ERROR: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
1>------
1>------
1> > [internal] load metadata for docker.io/microsoft/dotnet:2.2-aspnetcore-runtime:
1>failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
1>C:\Users\gopalk\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.4.10\build\Container.targets(258,5): error CTP1001: An error occurred while attempting to build Docker image.
1>Done building project "SampleApp.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
.NET Core version 2.2 is not supported anymore, the current LTS .NET version is 6.0. You can see the existing image versions of ASP.NET Core runtime on docker hub.
Updating .NET version on your machine:
If you are using Visual Studio you can use the Visual Studio Installer to install .NET 6 and all the templates that come with it. Then you should either recreate your project with .NET version 6, or upgrade you existing projects to .NET 6 and then re-generate your dockerfile, You can generate a Dockerfile in Visual Studio by right clicking the project file --> add --> docker support, before these steps you should delete your existing dockerfile.
We are trying to build an iot edge module on windows 1809. Our Project folder hierarchy is as follows:
----BaseDir
|---ClassLibrary1
|---ClassLibrary2
|--references ClassLibrary1
|--references ClassLibrary3
|---ClassLibrary3
|--references ClassLibrary4
|--references ClassLibrary1
|---ClassLibrary4
|---OurIOTEdgeModule
|--references ClassLibrary2
Our docker file looks like this:
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
COPY ./NuGet.Config ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.1-runtime-nanoserver-1809
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "IOTEdgeModule.dll"]
When we build this IOT Edge Module we are not getting any build errors. When we deploy the module on the EDGE device, we are facing the issue.
Our IOTEdgeModule is referencing ClassLibrary2.csproj. ClassLibrary2 inturn is referencing ClassLibrary3.csproj which in turn is referencing ClassLibrary4.csproj. Our IOT Edge module is only able to get the reference of ClassLibrary2.csproj but failing to refer all other dependant *.csprojs.
How to modify the docker file to solve this issue? Suggestions would be much appreciated.
This is not possible with the dockerfiles provided by the IoT Edge templates. The official IoT Edge repo has examples with the same structure. Instead of building inside a docker container, they build outside and only copy the built binaries into the final image. See this example project file and the corresponding Dockerfile:
ARG base_tag=2.1.10-alpine3.7
FROM mcr.microsoft.com/dotnet/core/runtime:${base_tag}
ARG EXE_DIR=.
ENV MODULE_NAME "SimulatedTemperatureSensor.dll"
WORKDIR /app
COPY $EXE_DIR/ ./
# Add an unprivileged user account for running the module
RUN adduser -Ds /bin/sh moduleuser
USER moduleuser
CMD echo "$(date --utc +"[%Y-%m-%d %H:%M:%S %:z]"): Starting Module" && \
exec /usr/bin/dotnet SimulatedTemperatureSensor.dll
Update: There is a solution to a similar issue here.
I've got a dotnet core project running AspNetCore webserver. I use a couple of other DLLs which is are fairly simple class libaries.
I can download the repository from git onto my windows PC, go in and run:
dotnet restore
dotnet run
And everything works fine.
However if I do the same thing in a docker container based on microsoft/aspnetcore-build:1.0.7, I get the following error on an HTTP PUT:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLBKHRVH7OND": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'KolData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Now the file Koldata.dll does exist in the git repository and it's there in the bin/Debug/netcoreapp1.1 folder.
I can re-create the error in Windows by deleting the KolData.dll file in the build directory. So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.
I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.
One solution
I managed to get it working by changing the csproj file's target framework from:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
to:
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
This feels a bit strange since KolData.dll is running on 1.1
But now it runs without the error.
You have to create Dockerfile and build docker image.
EDIT:
An example of what Dockerfile should look like. The following file builds Visual Studio project and creates docker image.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication/WebApplication.csproj WebApplication/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
If you have already built your application include .dll in container with Dockerfile:
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "application.dll"]
Build and run your app:
docker build -t application
docker run -d -p 8000:80 application
Building Docker Images for .NET Core Applications