.NET Core 3.1 Worker Service - 在发布时设置 EnvironmentName
.NET Core 3.1 Worker Service - set EnvironmentName on publish
我正在尝试使用 VS 2019 Enterprise 提供的 asp.net 核心 3.1 模板设置辅助服务。到目前为止:
1 / 创建了一个新项目,运行 它:"Hosting Environment: Development",按预期工作。
发展
2 / 创建了文件系统发布配置文件,并添加了 <EnvironmentName>Test</EnvironmentName>
以将 worker 发布到测试环境,例如
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<EnvironmentName>Test</EnvironmentName>
</PropertyGroup>
</Project>
启动已发布版本时,HostingEnvironment 设置为 "Production",但我在 FolderProfile.pubxml
中定义了 "Test"
生产
我试过 dotnet publish /p:EnvironmentName=Test
,但没有成功。
我还尝试手动生成一个 web.config 文件,其中包含
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Test" />
辅助服务似乎忽略了 web.config 文件。
它在调试时在 VS 上工作,具有 DOTNET_ENVIRONMENT = Test
测试
在不同的环境中发布 worker 服务的正确方法是什么?
我推荐的方法是将同一个工件推送到您的所有环境并将环境变量 DOTNET_ENVIRONMENT
设置为您想要的值。
SET DOTNET_ENVIRONMENT=test2
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:27:34 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test2
另一种方法是在部署的同时发布一个 json 文件。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1
// using Microsoft.Extensions.Configuration;
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
});
您的 hostsettings.json 的内容可能是:
{
"Environment": "test"
}
现在您只需使用与所需目标相匹配的 json 文件,并将其复制到您的 DLL 旁边。
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:06:03 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test
info: Microsoft.Hosting.Lifetime[0]
我正在尝试使用 VS 2019 Enterprise 提供的 asp.net 核心 3.1 模板设置辅助服务。到目前为止:
1 / 创建了一个新项目,运行 它:"Hosting Environment: Development",按预期工作。
发展
2 / 创建了文件系统发布配置文件,并添加了 <EnvironmentName>Test</EnvironmentName>
以将 worker 发布到测试环境,例如
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<EnvironmentName>Test</EnvironmentName>
</PropertyGroup>
</Project>
启动已发布版本时,HostingEnvironment 设置为 "Production",但我在 FolderProfile.pubxml
中定义了 "Test"生产
我试过 dotnet publish /p:EnvironmentName=Test
,但没有成功。
我还尝试手动生成一个 web.config 文件,其中包含
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Test" />
辅助服务似乎忽略了 web.config 文件。
它在调试时在 VS 上工作,具有 DOTNET_ENVIRONMENT = Test
测试
在不同的环境中发布 worker 服务的正确方法是什么?
我推荐的方法是将同一个工件推送到您的所有环境并将环境变量 DOTNET_ENVIRONMENT
设置为您想要的值。
SET DOTNET_ENVIRONMENT=test2
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:27:34 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test2
另一种方法是在部署的同时发布一个 json 文件。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1
// using Microsoft.Extensions.Configuration;
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
});
您的 hostsettings.json 的内容可能是:
{
"Environment": "test"
}
现在您只需使用与所需目标相匹配的 json 文件,并将其复制到您的 DLL 旁边。
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:06:03 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test
info: Microsoft.Hosting.Lifetime[0]