我使用目标框架dnxcore50在ASP.NET Core中创建了WebAPI项目。我的解决方案中有四个项目。
我的应用程序正在使用Postgresql
数据库来存储数据,而在应用程序端,我使用dapper
和npgsql
nuget包与Postgresql
进行数据库操作。
所有内容都在Windows环境中运行,之后我尝试使用Ubuntu 14.04并面临“超时问题”#39;在select
中执行npgsql
查询时,我通过使用github issue中提到的解决方案解决了这个问题,之后一切都在Ubuntu机器中完美运行。
所以,我想让我们尝试在docker中运行WebAPI,为此我创建了docker镜像也成功运行了但不幸的是当我调用select API方法调用时,我得到一些新的(与ubuntu 14.04相比)错误域存储库,使用dapper
和npgsql
来从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
答案 0 :(得分:0)
如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
中添加运行时解决了我的问题。