Environment.GetEnvironmentVariable将找不到变量值

时间:2022-06-13 10:56:55

Why Environment.GetEnvironmentVariable("variableName") won't get variable's value if the call is made from within a webMethod hosted on iis and It will work if I call It from a console application on the same machine?

为什么如果从iis上托管的webMethod中进行调用,Environment.GetEnvironmentVariable(“variableName”)将不会获得变量的值,如果我从同一台机器上的控制台应用程序调用它,它将起作用?

Where do I setup those variables to be visible to IIS web services? Should I use the second parameter from Environment.GetEnvironmentVariable(name, target) to get it?

我在哪里设置这些变量以使IIS Web服务可见?我应该使用Environment.GetEnvironmentVariable(name,target)中的第二个参数来获取它吗?

It 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 set at system and user level.

顺便说一句,VARIABLE_NAME_HERE设置在系统和用户级别。

Thanks in advance

提前致谢

4 个解决方案

#1


8  

Read here for more information:

阅读此处了解更多信息:

Using System Wide Environment Variables in .NET Application

在.NET应用程序中使用系统范围的环境变量


Specifically:

特别:

What Are System Environment Variables?

什么是系统环境变量?

Environment variables are strings that save information about the entire environment in your system. These string values are dynamic and they can affect the way your system will behave on. Environment variables can be classified into two main types:

环境变量是保存有关系统中整个环境的信息的字符串。这些字符串值是动态的,它们可能会影响系统的行为方式。环境变量可以分为两种主要类型:

System Variables: They affect the entire system whatever the current user is. They are defined by Windows and saved in the registry. You need to be an administrator to be able to modify them. You usually need to restart your computer to make these changes effective.

系统变量:无论当前用户是什么,它们都会影响整个系统。它们由Windows定义并保存在注册表中。您需要成为管理员才能修改它们。您通常需要重新启动计算机才能使这些更改生效。

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

用户变量:它们会影响当前系统用户的当前环境。任何系统用户都可以删除,修改和添加它们。它们由Windows安装程序,某些程序和用户使用。对这些变量的更改将保存到注册表中并立即生效。


If you try to invoke an environment variable that does not exist on your machine you will have issues. You must be trying to find a variable that exists on your local machine, but not on your web service's host machine.

如果您尝试调用计算机上不存在的环境变量,则会出现问题。您必须尝试查找本地计算机上存在的变量,但不能在Web服务的主机上查找。

#2


15  

I faced the same issue, and thanks to @sergserg I came up with this and it worked:

我遇到了同样的问题,感谢@sergserg,我提出了这个并且它有效:

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

The important bit was using EnvironmentVariableTarget.User

重要的是使用EnvironmentVariableTarget.User

#3


6  

Your need to restart the IIS by using iisreset command.

您需要使用iisreset命令重新启动IIS。

#4


5  

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

重新启动Visual Studio为我修复了它(猜测IIS Express也会缓存这些值)。

#1


8  

Read here for more information:

阅读此处了解更多信息:

Using System Wide Environment Variables in .NET Application

在.NET应用程序中使用系统范围的环境变量


Specifically:

特别:

What Are System Environment Variables?

什么是系统环境变量?

Environment variables are strings that save information about the entire environment in your system. These string values are dynamic and they can affect the way your system will behave on. Environment variables can be classified into two main types:

环境变量是保存有关系统中整个环境的信息的字符串。这些字符串值是动态的,它们可能会影响系统的行为方式。环境变量可以分为两种主要类型:

System Variables: They affect the entire system whatever the current user is. They are defined by Windows and saved in the registry. You need to be an administrator to be able to modify them. You usually need to restart your computer to make these changes effective.

系统变量:无论当前用户是什么,它们都会影响整个系统。它们由Windows定义并保存在注册表中。您需要成为管理员才能修改它们。您通常需要重新启动计算机才能使这些更改生效。

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

用户变量:它们会影响当前系统用户的当前环境。任何系统用户都可以删除,修改和添加它们。它们由Windows安装程序,某些程序和用户使用。对这些变量的更改将保存到注册表中并立即生效。


If you try to invoke an environment variable that does not exist on your machine you will have issues. You must be trying to find a variable that exists on your local machine, but not on your web service's host machine.

如果您尝试调用计算机上不存在的环境变量,则会出现问题。您必须尝试查找本地计算机上存在的变量,但不能在Web服务的主机上查找。

#2


15  

I faced the same issue, and thanks to @sergserg I came up with this and it worked:

我遇到了同样的问题,感谢@sergserg,我提出了这个并且它有效:

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

The important bit was using EnvironmentVariableTarget.User

重要的是使用EnvironmentVariableTarget.User

#3


6  

Your need to restart the IIS by using iisreset command.

您需要使用iisreset命令重新启动IIS。

#4


5  

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

重新启动Visual Studio为我修复了它(猜测IIS Express也会缓存这些值)。