gpt4 book ai didi

c# - Docker容器中的.Net Core 3.1应用程序环境变量不起作用

转载 作者:行者123 更新时间:2023-12-02 19:18:40 24 4
gpt4 key购买 nike

我正在尝试在docker容器中本地运行.Net Core 3.1 Web API套件。如果我在docker run命令(docker run -e ....)中输入环境变量,一切都会正常进行。但是,当我尝试通过--env-file(作为一个文件,因为会有很多)将它们传递时,它的行为很奇怪。当我在控制台日志中记录从文件中提取的变量时,它可以正常工作。但是当我使用USE变量时,它的作用是,如果变量不存在/抛出错误。这个例子碰巧被转换为int,但是我看到字符串也有同样的问题。我可以注销它们,但是当我去使用它们或操纵它们时,就像它们不存在一样。有人看过这样的东西吗?

C#

var testPort = Environment.GetEnvironmentVariable("TestPort");

Console.WriteLine("Testing Port: " + testPort);

var testPortInt = Convert.ToInt32(testPort);

Docker配置文件(dockerlocal.env)
TestPort="1234"

正在运行以构建/启动的Docker命令
docker build --no-cache -t telematics-web-api -f Dockerfile .

docker run --name telematics-web-api -p 8080:80 --env-file dockerlocal.env telematics-web-api

Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build

WORKDIR /build

COPY . .

RUN dotnet restore && dotnet publish -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS final

WORKDIR app

COPY --from=build /app .

EXPOSE 443

EXPOSE 80

ENTRYPOINT ["dotnet", "Telematics.Web.API.dll"]

输出
Testing Port: "1234"
Unhandled exception. System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at Fluid.Web.API.Startup.ConfigureServices(IServiceCollection services) in /build/Telematics.Web.API/Startup.cs:line 94
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Fluid.Web.API.Program.BuildWebHost(String[] args) in /build/Telematics.Web.API/Program.cs:line 24
at Fluid.Web.API.Program.Main(String[] args) in /build/Telematics.Web.API/Program.cs:line 21

编辑:

字符串也会发生。以下代码引发有关主机为空的错误。但是它仍然将URL写入输出。当我尝试使用docker run的--env-file参数时,会发生这种情况。如果我使用-e参数并在命令行中传递相同的值,而不是通过文件传递,则可以正常工作

C#
var url = Environment.GetEnvironmentVariable("AuthUrl"); 

Console.WriteLine(url);

HttpRequestMessage request = new
HttpRequestMessage(HttpMethod.Post, url);

request.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body));
request.Content.Headers.Remove("Content-Type");
request.Content.Headers.Add("Content-Type", "application/vnd.api+json");
request.Content.Headers.Add("version", "1.6");

var resp = client.SendAsync(request).GetAwaiter().GetResult();

Docker配置文件(dockerlocal.env)
AuthUrl="https://..............."

Docker命令和Dockerfile与上面的相同

输出
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
Route matched with {action = "Login", controller = "Account"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Login(System.String, System.String) on controller Fluid.Web.API.Controllers.AccountController (Telematics.Web.API).
"https://URLFROMDOCKERCONFIGFILE"
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
Executed action Fluid.Web.API.Controllers.AccountController.Login (Telematics.Web.API) in 86.2836ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'Fluid.Web.API.Controllers.AccountController.Login (Telematics.Web.API)'
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLVFFOERK3K8", Request id "0HLVFFOERK3K8:00000004": An unhandled exception was thrown by the application.
System.InvalidOperationException: An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.
at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request)
at Fluid.Web.API.Controllers.AccountController.Login(String userName, String password) in /build/Telematics.Web.API/Controllers/AccountController.cs:line 76

最佳答案

更改dockerlocal.env的文件内容

TestPort="1234"
AuthUrl="https://..............."


TestPort=1234
AuthUrl=https://...............

你们都准备好了。

关于c# - Docker容器中的.Net Core 3.1应用程序环境变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61600385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com