小编Doj*_*owl的帖子

docker-compose 在我的 C# 测试中找不到我的环境变量

我有一个简单的 C# 应用程序,它使用 docker-compose 运行 API。

当使用 docker-compose 在我的本地机器上运行测试时,我收到一个连接字符串错误,因为 docker 没有找到我的环境变量。

我的测试使用 XUnit 运行,我使用 XUnit 依赖注入方法和启动文件,在这个文件中注入了连接字符串。

我不知道我缺少什么,很乐意获得帮助!

.env 文件:

ConnectionStrings__Default=MyDatabaseConnection
Run Code Online (Sandbox Code Playgroud)

在 yaml 文件中,这是与环境变量相关的代码。

env_file:
  - .env
environment:
  - ACCEPT_EULA=Y
  - ConnectionStrings__Default=${ConnectionStrings__Default}
Run Code Online (Sandbox Code Playgroud)

在测试启动类中

public void ConfigureServices(IServiceCollection services)
{
    Configuration = new ConfigurationBuilder().Build();
    services.AddDbContext<SqlDbContext>(
        options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
}
Run Code Online (Sandbox Code Playgroud)

c# xunit docker docker-compose asp.net-core

4
推荐指数
1
解决办法
69
查看次数

标签 统计

asp.net-core ×1

c# ×1

docker ×1

docker-compose ×1

xunit ×1