【发布时间】:2020-09-18 08:22:50
【问题描述】:
我正在使用 docker 容器在 azure-pipeline 中运行我的 XUnit 测试。我为每个 .NET Core 单元测试项目都有一个 Dockerfile。我按照这里详述的模式:
Running your unit tests with Visual Studio Team Services and Docker Compose
我能够运行所有单元测试项目,但我使用以下参考的项目除外:
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite。
我在内存中使用 SQLite。
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var option = new DbContextOptionsBuilder<Context>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;
var dbContext = new Context(option, null);
最初,我的 DockerFile 设置如下:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
但是,在映像中构建和运行时,我收到以下错误:
“无法加载共享库 'libsqlite3-mod-spatialite' 或其依赖项之一。”
单元测试在 Visual Studio 测试运行器中运行良好,只是在映像中运行时不行。
经过研究,我更改了我的 Dockerfile 以安装 spatiallite。
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
RUN apt-get update && apt-get install -y \
libsqlite3-mod-spatialite
我收到以下新错误:
活动测试运行已中止。原因:测试主机进程崩溃。
在将 SQLite 与空间数据一起使用时,我尝试按照 Microsoft 的建议创建自定义 SQLitePCLRaw 提供程序。
Microsoft Documentation on Spatial Data
public class NativeLibraryAdapter : IGetFunctionPointer
{
readonly IntPtr _library;
public NativeLibraryAdapter(string name)
=> _library = NativeLibrary.Load(name);
public IntPtr GetFunctionPointer(string name)
=> NativeLibrary.TryGetExport(_library, name, out var address)
? address
: IntPtr.Zero;
}
And in my SQLite configuration:
SQLite3Provider_dynamic_cdecl
.Setup("sqlite3", new NativeLibraryAdapter("sqlite3"));
SQLitePCL.raw.SetProvider(new SQLite3Provider_dynamic_cdecl());
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var option = new DbContextOptionsBuilder<EmployeeContext>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;
现在我收到以下错误: “无法加载共享库 'sqlite3' 或其依赖项之一。”
这发生在 Visual Studio 测试运行程序和运行我的 Docker 映像时。
在这一点上,我不确定我是否采取了正确的方法来使其正常工作。任何指导表示赞赏。
【问题讨论】:
-
你可能也需要
apt-get install libsqlite3-dev -
另外,现在你可以只安装
SQLitePCLRaw.provider.sqlite3包并使用SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3())而不是自己实现它。
标签: c# sqlite docker docker-compose spatial