.NET Core 3.1 Worker Service - set EnvironmentName on publish

Viewed 6462

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

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

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

Test

What's the correct way to publish worker services on different environments ?

2 Answers

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]

For any one that stumbles over this question I hade the same issue.

Going through Host.CreateDefaultBuilder source code it shows we can set DOTNET_[Variable] from args passed

so to run your service in Development run the following command

dotnet DLL:

dotent [DllName].dll --ENVIRONMENT=Development

exe:

[ExeName].exe --ENVIRONMENT=Development

Dotnet core source:

DotNet Core Source Code

Related