Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 3.1 Worker Service - set EnvironmentName on publish

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 ?

like image 768
Karuskrokro Avatar asked Mar 18 '20 14:03

Karuskrokro


People also ask

How do you check if current environment is development or not?

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).

How do I view environment variables in .NET Core?

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.


1 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]
like image 82
Alex AIT Avatar answered Sep 25 '22 17:09

Alex AIT