【问题标题】:System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Security, Version=4.0.0.0System.IO.FileNotFoundException:无法加载文件或程序集'System.Net.Security,版本 = 4.0.0.0
【发布时间】:2016-05-04 23:46:03
【问题描述】:

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

  • WebAPI - 包含 API 方法
  • 核心 - 包含 IRepository、IDataContext 接口和基类,以使用 dapper 与 Postgresql 进行通信
  • DataAccess - 包含域的存储库类
  • 域 - 包含域模型

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

一切都在 Windows 环境中运行,之后我在 Ubuntu 14.04 中尝试并在 npgsql 中执行 select 查询时遇到了“超时问题”,但我通过使用 github issue 中提到的解决方案解决了这个问题,然后一切在 Ubuntu 机器上也能完美运行。

所以,我想让我们尝试在 docker 中运行 WebAPI,为此我还创建了 docker 映像并且也能够成功运行,但不幸的是,当我调用 select API 方法时,我得到了一些新的(与 ubuntu 14.04 相比)错误,该方法调用使用 dappernpgsql 从 Postgrespl 中选择数据的域存储库。

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

【问题讨论】:

标签: c# postgresql ubuntu docker asp.net-core-1.0


【解决方案1】:

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

注意 aspnet docker image 已经有ENV DNX_RUNTIME_ID ubuntu.14.04-x64 行,但不知道为什么这还不够,需要在dnu restore 中设置运行时。

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

【讨论】:

    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多