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

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Security, Version=4.0.0.0

我已经在 ASP.NET Core 中使用目标框架 dnxcore50 创建了 WebAPI 项目。我的解决方案中有四个项目。

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

一切都在 windows 环境中工作,之后我在 Ubuntu 14.04 中尝试并在 npgsql 中执行 select 查询时面对 'Timeout issue' 但我通过使用 github issue 中提到的解决方案解决了这个问题,之后一切都在 Ubuntu 机器上完美运行。

所以,我想让我们在 docker 中尝试 运行 WebAPI 为此,我还创建了 docker 图像并成功地能够 运行同样但不幸的是,当我调用 select API 方法时,我遇到了一些新的(与 ubuntu 14.04 相比)错误,该方法通过使用 dappernpgsql.

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()

下面是解决方案中使用的一些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 创建的本地 docker 图像文件 hub.docker.com 没有那个。

global.json

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

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

如果需要更多信息,请告诉我。

附加信息

ubuntu

中的dnx版本信息
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 中所述,我将 docker 文件中的 RUN ["dnu", "restore"] 行更改为 RUN ["dnu", "restore","--runtime=ubuntu.14.04-x64"],问题得到解决。

注意 aspnet docker 图像已经有行 ENV DNX_RUNTIME_ID ubuntu.14.04-x64 但不知道为什么这还不够,需要在 [= 中设置运行时13=].

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