Dotnet Linux Docker容器在开发配置下工作,但在本地配置下不工作。

huangapple go评论78阅读模式
英文:

Dotnet Linux Docker container works with Development config but not Local Config

问题

我正在尝试将一个ASP.Net Core WebAPI项目设置为Linux Docker容器。它在容器外部目前可以正常运行。

当我尝试在本地运行容器时,API无法启动,并在调用application.Run()方法时抛出以下错误。

System.InvalidOperationException: '无法配置HTTPS端点。未指定服务器证书,并且默认的开发者证书找不到或已过期。要生成开发者证书,请运行'dotnet dev-certs https'。要信任证书(仅适用于Windows和macOS),请运行'dotnet dev-certs https --trust'。有关配置HTTPS的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=848054。'

如果我更改我的Docker配置文件中的配置环境,指向我的开发测试环境的配置设置,那么一切在本地都正常工作(尽管指向测试环境的资源)。

容器是一个Linux容器,因此无法访问dotnet dev-certs https --trust。但是,我觉得如果是证书问题,只需通过更改ASPNET_ENVIRONMENT设置指向不同的配置设置就不会“修复”它。

在设置应用程序的配置值时,它首先从我的开发测试环境中提取配置设置,然后用本地配置文件中的设置进行覆盖。如果我将本地配置文件中的所有设置注释掉,仍然会出现相同的错误。

英文:

I am attempting to setup an ASP.Net core WebAPI project into a Linux Docker container. It currently works fine outside of the container.

When I attempt to run the container locally, the API will not start and throw the following error when calling the application.Run() method.

System.InvalidOperationException: 'Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.'

If I change the configuration environment in my docker-config file to point to my development testing environment's configuration settings, everything works just fine locally (though pointing to the testing environment's resources).

The container is a Linux container so it does not have access to dotnet dev-certs https --trust. However, I feel like if it was a certificate issue, it wouldn't be "fixed" by just pointing it to different configuration settings simply by changing the ASPNET_ENVIRONMENT setting.

When setting up the configuration values for application, it pulls in the configuration settings from my development testing environment and then overrides them with settings in the local configuration file. If I comment out all settings in the local configuration file, the same error persists.

答案1

得分: 0

在进行了大量的故障排除和搜索后,我找到了解决此问题的方法。

ASP.Net Core 在环境为 Development 时会硬编码设置默认的 https 证书。要更新此设置以在任何其他环境中运行,请将以下代码段添加到您的 Program.cs 文件中。

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    if (hostingContext.HostingEnvironment.IsEnvironment("LocalDocker"))
    {
        config.AddUserSecrets<Program>();
    }
})
英文:

After extensive troubleshooting and searching, I found the solution to this issue.

ASP.Net Core has a hardcoded, compiled setting where it sets up a default https cert when the environment is Development. To update this setting for any other environment you wish to run in, you can add the following snippet into your Program.cs file.

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureAppConfiguration((hostingContext, config) =&gt; {
    if (hostingContext.HostingEnvironment.IsEnvironment(&quot;LocalDocker&quot;))
    {
        config.AddUserSecrets&lt;Program&gt;();
    }
})

huangapple
  • 本文由 发表于 2023年7月27日 22:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定