I'm trying to setup a worker service using the asp.net core 3.1 template provided by VS 2019 Enterprise. So far:
1 / Created a new project and ran it: "Hosting Environment: Development", works as expected.
Development
2 / Created a FileSystem publish profile, and added <EnvironmentName>Test</EnvironmentName>
to publish the worker to a testing environment for exemple
<?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>
When launching the published version, HostingEnvironment is set to "Production", but i defined "Test" in the FolderProfile.pubxml
Production
I've tried dotnet publish /p:EnvironmentName=Test
, without success.
I also tried to generate manually a web.config file, containing
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Test" />
The worker service seems to ignore the web.config file.
It works on VS while debugging, having DOTNET_ENVIRONMENT = Test
Test
What's the correct way to publish worker services on different environments ?
How do you check if current environment is development or not? If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).
Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below. You may change the value as per your need.
My recommended way would be to push the same artifact to all your environments and set the environment variable DOTNET_ENVIRONMENT
to your desired value.
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
Another way to do it would be to publish a json file alongside your deployment.
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);
});
The content of your hostsettings.json could be:
{
"Environment": "test"
}
Now you only need to use the json file that matches your desired target, and copy that next to your 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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With