How to publish .NET Core console application with particular profile?

65 Views Asked by At

I have console application in .NET Core. I have created profiles in launchSettings.json as shown here:

{
  "profiles": {
    "Console.Dev": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "DEV"
      }
    },
    "Console.UAT": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "UAT"
      }
    },
    "Console.PRD": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "PRD"
      }
    }
  }
}

Usually I publish application using "Publish" function of Visual Studio. i.e. right-click on the project, then click on "Publish"

enter image description here

But it does not have any option to select profile. I want to deploy with profile name as Console.PRD. How can I do that?

--Edited after answer from jepozdemir After I publish using dotnet cli, as per mentioned by jepozdemir

I found is that, it does not transform appsettings.json

example, I have appsettings.json as below

"AppSettings": {
    "env": "Common"
  }

then I have appsettings.dev.json as below

"AppSettings": {
    "env": "Dev"
  }

When I publish by selecting environment name as dev, I want this to transform appsettings.json and it would have "env":"Dev" But it is not working as expected, appsettings.json has "env":"common"

I want something like it transform app.config using SlowCheetah

Anything I am missing ?

2

There are 2 best solutions below

3
Pasindu Jay On

Published applications do not rely on the environment variable.

When running your application, you have the option to specify the ASPNETCORE_ENVIRONMENT environment variable on your server to access the corresponding configuration. Just as you added it to Visual Studio launch settings, you can configure it within your web server or set it up at the operating system level.

For example, if you're running your application within IIS, you may specify it within your web config file. Alternatively, you can set it up at the OS level. In Linux, for instance, you can export ASPNETCORE_ENVIRONMENT=RDP to set it up.

Update, Dotnet command supports a way to explicitly tell which environment to use. it automatically adds config inside the web config file so that you don't manually have to do that. https://stackoverflow.com/a/53056912/3333933

3
jepozdemir On

you can use dotnet cli, this will set ASPNETCORE_ENVIRONMENT in published web.config.

dotnet publish /p:EnvironmentName=PRD

Also check the official document for other options.

https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish