System.IO.FileNotFoundException:无法加载文件或程序集“System.Net.Security,Version = 4.0.0.0

我已经在ASP.NET Core中创build了具有目标框架dnxcore50的WebAPI项目。 我的解决scheme中有四个项目。

  • WebAPI – 包含API方法
  • Core – 包含IRepository,IDataContext接口和基类,以便与Dapper进行Postgresql通信
  • DataAccess – 包含域的存储库类
  • 域 – 包含域模型

我的应用程序使用Postgresql数据库来存储数据,而在应用程序端,我使用了dappernpgsql nuget包来执行Postgresql数据库操作。

一切都在Windows环境中工作,之后,我尝试在Ubuntu 14.04和遇到“超时问题”,同时在npgsql执行select查询,但我解决了,通过使用github问题中提到的解决scheme,并在此之后,一切工作完美的Ubuntu机器也。

所以,我想我们试图在Docker中运行WebAPI,为此我创build了Docker镜像,并成功地运行了,但不幸的是,当我调用select API方法调用域存储库的时候,我得到了一些新的(比较ubuntu 14.04)错误通过使用dappernpgsql从Postgrespl中select数据。

 fail: Microsoft.AspNet.Server.Kestrel[13] An unhandled exception was thrown by the application. System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. File name: 'System.Net.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load the specified file. File name: 'System.Net.Security' at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName) at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName) at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout) at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout) at Npgsql.NpgsqlConnector.Open() at Npgsql.NpgsqlConnectorPool.GetPooledConnector(NpgsqlConnection Connection) at Npgsql.NpgsqlConnectorPool.RequestConnector(NpgsqlConnection connection) at Npgsql.NpgsqlConnection.OpenInternal(NpgsqlTimeout timeout) at Npgsql.NpgsqlConnection.Open() 

下面是解决scheme中使用的一些project.json文件和docker文件。

核心project.json文件

 { "version": "1.0.0-*", "description": "Core Class Library", "authors": [ "Jignesh" ], "tags": [ "" ], "projectUrl": "", "licenseUrl": "", "dependencies": { "Dapper": "1.50.0-beta8", "Dapper.Contrib": "1.50.0-beta8", "Npgsql": "3.1.0-alpha6", "System.Net.Security": "4.0.0-beta-*", "Microsoft.NETCore.Platforms": "1.0.1-beta-*" }, "frameworks": { "dnxcore50": {} } } 

Docker文件

 FROM jignesh/aspnet:1.0.0-rc1-update2-coreclr RUN printf "deb http://ftp.us.debian.org/debian jessie main\n" >> /etc/apt/sources.list # Add the PostgreSQL PGP key to verify their Debian packages. # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 # Add PostgreSQL's repository. It contains the most recent stable release # of PostgreSQL, ``9.3``. RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3 # There are some warnings (in red) that show up during the build. You can hide # them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 COPY . /app WORKDIR /app/src/WebAPI RUN ["dnu", "restore"] EXPOSE 5002/tcp ENTRYPOINT ["dnx", "-p", ".", "web"] 

注意 jignesh / aspnet是我为1.0.0-rc1-update2-coreclr创build的本地docker映像文件,因为hub.docker.com没有这个文件。

global.json

 { "projects": [ "wrap", "src", "test" ], "sdk": { "version": "1.0.0-rc1-update2", "runtime": "coreclr", "architecture": "x64" } } 

有人可以帮忙吗? 我该怎么做才能解决这个问题。

让我知道是否需要更多的信息。

附加信息

在Ubuntu的DNN版本信息

 Microsoft .NET Execution environment Version: 1.0.0-rc1-16609 Type: CoreClr Architecture: x64 OS Name: Linux OS Version: ubuntu 14.04 Runtime Id: ubuntu.14.04-x64 

Docker中的dnx版本信息

 Microsoft .NET Execution environment Version: 1.0.0-rc1-16609 Type: CoreClr Architecture: x64 OS Name: Linux OS Version: 8 debian Runtime Id: ubuntu.14.04-x64 

正如在https://github.com/StackExchange/StackExchange.Redis/issues/350中提到的,我将RUN ["dnu", "restore"]行改为RUN ["dnu", "restore","--runtime=ubuntu.14.04-x64"]在dockerfile和问题得到解决。

注意 aspnet ENV DNX_RUNTIME_ID ubuntu.14.04-x64镜像已经有了line ENV DNX_RUNTIME_ID ubuntu.14.04-x64但是不知道为什么这只是不够的,并且需要在dnu restore设置运行时。

无论如何,我很高兴在dnu restore中添加运行时解决了我的问题。