0

We're having an issue right now where we're unable to debug our Dev server environment: enter image description here

Now, I've spent literal hours searching for how to set ASPNETCORE_ENVIRONMENT and I've only found two (practical) ways:

  1. Create a web.[MYCONFIGURATION].config file and add in the following, where [MYCONFIGURATION] would be something like Development or Release:

web.[MYCONFIGURATION].config:

<environmentVariables>
  <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
  1. Set IIS variables on the deployed server to enable ASPCORE_ENVIRONMENT

Here are the problems with each:

  1. Since .NET Core uses appsettings.json files for its configuration (a web.config is generated from it, so far as I understand), things like our database connection strings and other settings get lost if we create a web.config ourselves.
  2. We are running in an enterprise-level, multi-application environment with a combination of .NET Core and non-Core ASP.NET applications (which I do not have access to), so changing IIS's settings for our application only is not an option.

It should be noted that we are using TFS's Build/Release to publish to our Dev server. I had hoped that adding a BuildConfiguration variable of "development" would also work to set ASPNETCORE_ENVIRONMENT, but unfortunately it does not. enter image description here

What can I do to set ASPNETCORE_ENVIRONMENT for our application only without needing a web.config or IIS settings? Or, if that is presently impossible, how can I make one of the two options work, given the constraints I have described?

nbrosz
  • 874
  • 12
  • 18
  • Have you got the Environment Variable set on the Dev server? You can add it via System Properties, On the Advanced Tab, click on the Environment Variables Button. The variable is ASPNETCORE_ENVIRONMENT. – Derek Apr 05 '18 at 15:29
  • @Derek As I mentioned in my question, I don't have access to the Dev server and even if I did, I can't set environmental variables on it because it's serving multiple other web apps in a multi-app enterprise environment. – nbrosz Apr 05 '18 at 17:07
  • A build configuration is a collection of project settings that determines how your IDE will build your project. It won't set environmentVariable on your dev server. It seems your issue doesn't relate to TFS, you may remove TFS tag. – Cece Dong - MSFT Apr 06 '18 at 08:01
  • @CeceDong-MSFT Is there some way to set an environment variable without configuring IIS? – nbrosz Apr 06 '18 at 13:10
  • @CeceDong-MSFT I completely disagree that this is unrelated to TFS. The entire question relates to how to use the TFS build tools to configure the target environment and is the exact reason why I came to look at this thread myself. – Mike Sep 04 '18 at 01:29

3 Answers3

1

I don't think I'm following correctly, seems to me you're almost there. We have a similar setup and here's what my deploy looks like.

Essentially, you don't create web.config from appsettings.json. You tell your app (running on Kestrel behind IIS) which appsettings.[Env].json to use, depending on ASPNETCORE_ENVIRONMENT value set in web.config. Not really sure what you mean by

things like our database connection strings and other settings get lost if we create a web.config ourselves

All my deployment scripts are using PS and PS remoting via our build server (as a TFS user, you should be familiar with this), so in a nutshell, this is what happens:

// Where Env.Name is the runtime configuration - i.e. "Production"
$environmentVariable = [PSCustomObject]@{Name='ASPNETCORE_ENVIRONMENT'; Value=$using:Env.Name}

// This is how I add the actual value for ASPNETCORE_ENVIRONMENT in web.config
Add-WebConfigurationProperty $IISSite -Filter "system.webServer/aspNetCore" -Name environmentVariables -Value $environmentVariable

The last line adds

<aspNetCore processPath=".\PathToMyApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
    <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
    </environmentVariables>
</aspNetCore>

to web.config, and you're all set.

Milan Vidakovic
  • 436
  • 5
  • 10
  • I don't have a web.config. A web.config gets generated automatically for the .Net Core 2 application (on build, I presume), so since I don't have a web.config file, I can't put things _in_ the web.config file. I don't know why, but if I **do** create a web.config file on my own, it causes problems with the configurations that normally come from appsettings.json. And, again, I don't have access to the IIS server. I'm a dev in a multi-application, enterprise-level environment. I do not have any kind of admin access, nor do I know the first thing about IIS server administration. – nbrosz Apr 06 '18 at 13:07
  • I don't have a web.config either. Are you not able to access the deployment scripts? Are you using OTB tfs scripts? – Milan Vidakovic Apr 06 '18 at 13:15
  • The answer I ended up with turned out to be similar to yours, so although I didn't use the method you described (I'm not an experienced TFS user and I'm still not sure what "PS remoting" is), I've given it an up-vote since it sounds like a reasonable alternate approach. – nbrosz Apr 10 '18 at 15:23
  • Glad it helped, even as an idea. PowerShell remoting, is a powerful module that allows you access to remote machines (i.e. deployment servers), and enables you to execute PS scripts remotely on those machines (provided you have the privilege). Incredibly useful stuff if you plan on customizing your ci/cd scripts (which I'm sure you will, in time). – Milan Vidakovic Apr 10 '18 at 15:27
1

You can also use the "IIS Web App Manage" task to set the variables using the section Advanced and Additional appcmd.exe commands:

set config "APPNAME" -section:system.webServer/aspNetCore /+"environmentVariables.[name='ASPNETCORE_ENVIRONMENT',value='Staging']" /commit:site

However, multiple executions of this script are not working. Maybe anyone has an idea?

See also: How to set environment variables per site on IIS using appcmd.exe?

MovGP0
  • 7,267
  • 3
  • 49
  • 42
0

Since I didn't have access to the server, the server admins refused to set a global environment variable across a multi-app server, and I couldn't pre-modify a web.config file in Visual Studio pre-release, the admins found an alternate solution.

Since I'm building/releasing using TFS, they added a Patch XML File task to TFS and instructed me to configure the task as seen below:

enter image description here

+ /configuration/system.webServer/aspNetCore/- => "environmentVariables"
+ /configuration/system.webServer/aspNetCore/environmentVariables/- => "environmentVariable"
= /configuration/system.webServer/aspNetCore/environmentVariables/environmentVariable/@name => "ASPNETCORE_ENVIRONMENT"
= /configuration/system.webServer/aspNetCore/environmentVariables/environmentVariable/@value=> "Development"

It seems to have worked just fine:

enter image description here

I must say it is irritating that this was my only recourse in .Net Core 2.0. Apparently being a server admin or having access to one who is very willing to update server configurations is a requirement to properly using .Net Core 2. Not impressed.

nbrosz
  • 874
  • 12
  • 18