Environment.GetEnvironmentVariable will not find the value of the variable

Why won't Environment.GetEnvironmentVariable ("variableName") get the value of the variable if the call is made from webMethod hosted in iis and will work if I call it from the console application on the same computer?

Where can I configure these variables for visibility for IIS web services? Should I use the second parameter from Environment.GetEnvironmentVariable (name, purpose) to get it?

This is actually really simple:

<code> [WebMethod(Description = "Gets the environment variable value.")] public string GetEnvironmentVariable() { return Environment.GetEnvironmentVariable("VARIABLE_NAME_HERE"); } </code> 

And by the way, VARIABLE_NAME_HERE is installed at the system and user level.

Thanks in advance

+8
c # web-services environment-variables iis
source share
4 answers

Read here for more information:

Using System Wide environment variables in a .NET application


In particular:

What are the system environment variables?

Environment variables are strings that store information about the entire environment on your system. These string values ​​are dynamic and they can affect the behavior of your system. Environment variables can be divided into two main types:

System variables They affect the entire system regardless of the current user. They are determined by Windows and stored in the registry. You must be an administrator in order to be able to modify them. You usually need to restart your computer to make these changes effective.

User Variables They affect the current environment of the current system user. They can be deleted, modified and added by any user system. They are used by the installation of Windows by some programs and users. Changes to these variables are stored in the registry and are effective immediately.


If you try to call an environment variable that does not exist on your computer, you will have problems. You should try to find a variable that exists on your local machine, but not on the main web service.

+6
source share

I ran into the same problem, and thanks to @sergserg, I came up with this and it worked:

 var value = Environment.GetEnvironmentVariable(key, EnvironmentVariableTarget.User) 

The important bit used EnvironmentVariableTarget.User

+8
source share

You must restart IIS using the iisreset command.

+5
source share

Restarting Visual Studio fixed it for me (assuming IIS Express also caches these values).

+2
source share

All Articles