'Environment specific config not loaded when passed as an argument - "Unhandled exception Failed to map app settings section"

I am trying to deploy a dotnet core 3.0 Windows service on a new Windows server. I have a ps1 script that creates the service like so:

$name = "DataTransformService"
$params = @{
  Name           = $name
  BinaryPathName = '"C:\services\transformService\DataTransform.Service.exe Test"'
  DisplayName    = $name
  StartupType    = "Automatic"
}
New-Service @params

However, when I try to start the service using the Services application on Windows and start it I get this error: enter image description here

When I tried to manually invoke the service using the binary:

C:\services\transformService\DataTransform.Service.exe Test

I get this error:

PS C:\services\transformService> C:\services\transformService\DataTransform.Service.exe Test
Configured for environment Test_ENVIRONMENT
Unhandled exception. System.MissingFieldException: Failed to map app settings section 'ConnectionStrings' as it wasn't found in appsettings.json or appsettings.Production.json. Add this section or remove class 'ConnectionStringsSettings' from the 'Core.Settings' namespace.
   at DataTransform.Service.StartupConfiguration.ConfigureSettingsExtensions.ConfigureSettingSection[T](IServiceCollection services, IConfiguration configuration, IHostEnvironment hostingEnvironment) in D:\_Code\DataTransform.Service\DataTransform.Service\StartupConfiguration\ConfigureSettingsExtensions.cs:line 48
   at DataTransform.Service.StartupConfiguration.ConfigureSettingsExtensions.ConfigureSettings(IServiceCollection services, IConfiguration configuration, IHostEnvironment hostingEnvironment) in D:\_Code\DataTransform.Service\DataTransform.Service\StartupConfiguration\ConfigureSettingsExtensions.cs:line 26
   at DataTransform.Service.Program.<>c.<CreateHostBuilder>b__1_2(HostBuilderContext hostContext, IServiceCollection services) in D:\_Code\DataTransform.Service\DataTransform.Service\Program.cs:line 47
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at DataTransform.Service.Program.Main(String[] args) in D:\_Code\DataTransform.Service\DataTransform.Service\Program.cs:line 15

I have a few environments defined such as below:

enter image description here

This is my Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args)
            .Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureHostConfiguration((configBuilder) =>
            {
                if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && !string.IsNullOrWhiteSpace(args[0]))
                {
                    Console.WriteLine($"Configured for environment {args[0]}_ENVIRONMENT");
                    configBuilder.AddEnvironmentVariables($"{args[0]}_");
                }
                else
                {
                    var environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
                    Console.WriteLine($"Configured for environment {environmentName}");
                    configBuilder.AddEnvironmentVariables();
                }
            })
            .ConfigureLogging(...)
            .ConfigureServices(...);
}


this is my appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  },
  "Worker": {
    "TargetDatabaseName": "TransformedData",
    ...
  }
}

this is appsettings.Test.json

{
  "ConnectionStrings": {
    <connection strings here>
  },
  "Environment": {
    "Name": "Test"
  },
  "Worker": {
    ...
  }
}

What am I missing here?



Solution 1:[1]

for collation error, you need to follow the following steps

1, go to config/database.php file
2, set charset="utf8" and collation="utf8_general_ci"

final MySQL array show like below

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_general_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1