简体   繁体   English

AspNetCore 项目仅在 linux 容器上提供 System.IO.FileNotFoundException

[英]AspNetCore project gives System.IO.FileNotFoundException only on linux container

I've got a dotnet core project running AspNetCore webserver.我有一个运行 AspNetCore 网络服务器的 dotnet 核心项目。 I use a couple of other DLLs which is are fairly simple class libaries.我使用了几个其他 DLL,它们是相当简单的类库。

I can download the repository from git onto my windows PC, go in and run:我可以从 git 将存储库下载到我的 Windows PC 上,进入并运行:

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:但是,如果我在基于 microsoft/aspnetcore-build:1.0.7 的 docker 容器中做同样的事情,我会在 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.现在文件 Koldata.dll 确实存在于 git 存储库中,它位于 bin/Debug/netcoreapp1.1 文件夹中。

I can re-create the error in Windows by deleting the KolData.dll file in the build directory.我可以通过删除构建目录中的 KolData.dll 文件在 Windows 中重新创建错误。 So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.因此,Linux 上的 dotnet 核心似乎无法看到该文件,我不确定为什么。

I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.我什至尝试用从源代码构建在机器上的版本替换 DLL,但它仍然带来相同的错误。

One solution一种解决方案

I managed to get it working by changing the csproj file's target framework from:我设法通过从以下位置更改 csproj 文件的目标框架来使其工作:

<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.这感觉有点奇怪,因为 KolData.dll 在 1.1 上运行但现在它运行没有错误。

You have to create Dockerfile and build docker image.您必须创建 Dockerfile 并构建 docker 镜像。

EDIT: An example of what Dockerfile should look like.编辑:Dockerfile 应该是什么样子的一个例子。 The following file builds Visual Studio project and creates docker image.以下文件构建 Visual Studio 项目并创建 docker 映像。

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:如果您已经使用 Dockerfile 在容器中构建了包含 .dll 的应用程序:

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 为 .NET Core 应用程序构建 Docker 镜像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM