我正在尝试将一个.NET核心2.0应用程序(使用身份服务器4的MVC应用程序)从MacOS上进行Docker化。我无法找到有关此错误消息的任何信息:
MacBook-Pro-3:myproject myuser$ sudo docker build -t mycompany/myproject .
Password:
Sending build context to Docker daemon 122.6MB
Step 1/10 : FROM microsoft/aspnetcore-build:2.0 AS build-env
---> 76ce1481a4b8
Step 2/10 : WORKDIR /app
---> Using cache
---> e4ac320f97de
Step 3/10 : COPY *.csproj ./
---> Using cache
---> d6e02527b792
Step 4/10 : RUN dotnet restore
---> Running in b3545ba0375f
System.BadImageFormatException: Could not load file or assembly 'System.Security.Cryptography.Primitives, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. An attempt was made to load a program with an incorrect format.
File name: 'System.Security.Cryptography.Primitives, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Microsoft.DotNet.Cli.Telemetry.Sha256Hasher.HashWithNormalizedCasing(String text)
at Microsoft.DotNet.Cli.Utils.ApplicationInsightsEntryFormat.<>c__DisplayClass10_0.<WithAppliedToPropertiesValue>b__1(KeyValuePair`2 p)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at Microsoft.DotNet.Cli.Utils.ApplicationInsightsEntryFormat.WithAppliedToPropertiesValue(Func`2 func)
at Microsoft.DotNet.Cli.Telemetry.TelemetryFilter.<Filter>b__3_0(ApplicationInsightsEntryFormat r)
at System.Linq.Enumerable.SelectListIterator`2.ToList()
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.DotNet.Cli.Telemetry.TelemetryFilter.Filter(Object objectToFilter)
at Microsoft.DotNet.Cli.Utils.TelemetryEventEntry.SendFiltered(Object o)
at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
at Microsoft.DotNet.Cli.Program.Main(String[] args)
The command '/bin/sh -c dotnet restore' returned a non-zero code: 1这个应用程序在我的机器上构建和运行良好(在docker之外)--只有在运行“/myproject”时才会出现这种情况。
这是我的Dockerfile:
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 .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]如果这有帮助,下面是web应用程序的.csproj文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>some-guid-000-0000-0000-00</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Consul" Version="0.7.2.4" />
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\MyCompany.Core.Model\MyCompany.Core.Model.csproj" />
<ProjectReference Include="..\MyCompany.Core.Contract\MyCompany.Core.Contract.csproj" />
<ProjectReference Include="..\MyCompany.Core.Service\MyCompany.Core.Service.csproj" />
<ProjectReference Include="..\MyCompany.Core.ViewModel\MyCompany.Core.ViewModel.csproj" />
<ProjectReference Include="..\MyCompany.Core\MyCompany.Core.csproj" />
<ProjectReference Include="..\MyCompany.Data.Contract\MyCompany.Data.Contract.csproj" />
<ProjectReference Include="..\MyCompany.Data.Model\MyCompany.Data.Model.csproj" />
<ProjectReference Include="..\MyCompany.Data\MyCompany.Data.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Scripts\view\" />
<Folder Include="Scripts\view\home\" />
</ItemGroup>
</Project>UPDATE:我还尝试在Dockerfile中使用这个构建目标:
FROM microsoft/dotnet:latest AS build-env初始错误消失了,但是在相同的构建步骤上出现了新的问题:
Step 1/10 : FROM microsoft/dotnet:latest AS build-env
latest: Pulling from microsoft/dotnet
Digest: sha256:580c65dd8bbbd75ba0ee74fff146cb44901f9be0ef233cd9d1399e1f9a8cfb11
Status: Downloaded newer image for microsoft/dotnet:latest
---> ae53eff0f099
Step 2/10 : WORKDIR /app
---> Using cache
---> 53551996e135
Step 3/10 : COPY *.csproj ./
---> Using cache
---> 1729e42c4ef1
Step 4/10 : RUN dotnet restore
---> Running in 6b3f62681fc6
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Core.Model/MyCompany.Core.Model.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Core.Contract/MyCompany.Core.Contract.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Core.Service/MyCompany.Core.Service.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Core.ViewModel/MyCompany.Core.ViewModel.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Core/MyCompany.Core.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Data.Contract/MyCompany.Data.Contract.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Data.Model/MyCompany.Data.Model.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(895,5): warning MSB3202: The project file "/MyCompany.Data/MyCompany.Data.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Core.Model/MyCompany.Core.Model.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Core.Contract/MyCompany.Core.Contract.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Core.Service/MyCompany.Core.Service.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Core.ViewModel/MyCompany.Core.ViewModel.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Core/MyCompany.Core.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Data.Contract/MyCompany.Data.Contract.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Data.Model/MyCompany.Data.Model.csproj" was not found. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(986,5): warning MSB3202: The project file "/MyCompany.Data/MyCompany.Data.csproj" was not found. [/app/MyCompany.csproj]
Restoring packages for /app/MyCompany.csproj...
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(104,5): error : An error occurred while retrieving package metadata for 'System.Security.Principal.Windows.4.3.0' from source '/usr/share/dotnet/sdk/NuGetFallbackFolder'. [/app/MyCompany.csproj]
/usr/share/dotnet/sdk/2.1.101/NuGet.targets(104,5): error : Invalid character in the given encoding. Line 1, position 1. [/app/MyCompany.csproj]
Restoring packages for /app/MyCompany.csproj...
The command '/bin/sh -c dotnet restore' returned a non-zero code: 1发布于 2018-03-20 11:26:41
我在我公司的生产环境中犯了这个错误。与安装的.Net核心的多个版本以及与.Net标准版本不匹配有关。
在您的计算机中,它可以工作,因为它使用的是正确的版本,因为当您安装.Net Core时,最新的版本被设置为默认版本。
但是在您的解决方案中,它指向另一个包版本。作为解决办法,你可以做的是:

您还可以更新从解决方案到与核心版本匹配的正确.Net标准版本的每个包。
https://stackoverflow.com/questions/49374491
复制相似问题