환경 변수 <환경변수>
개요
<environmentVariables>
요소의 <applicationPoolDefaults>
요소는 애플리케이션이 시작될 때 IIS(인터넷 정보 서비스) 10이 작업자 프로세스에 전달할 환경 변수 목록을 지정합니다. 요소 <environmentVariables>
에는 각 환경 변수에 <add>
대한 개별 이름/값 쌍을 정의하는 요소 컬렉션이 포함되어 있습니다.
호환성
버전 | 참고 |
---|---|
IIS 10.0 | 요소는 <environmentVariables> IIS 10에서 도입되었습니다. |
IIS 8.0 | 해당 없음 |
IIS 7.5 | 해당 없음 |
IIS 7.0 | 해당 없음 |
IIS 6.0 | 해당 없음 |
설치 프로그램
요소는 <environmentVariables>
IIS 10의 기본 설치에 포함됩니다.
방법
IIS 10에 대한 환경 변수를 설정하기 위한 사용자 인터페이스는 없습니다. 환경 변수를 프로그래밍 방식으로 설정하는 방법에 대한 예제는 이 문서의 코드 샘플 섹션을 참조하세요.
구성
특성
없음
자식 요소
요소 | Description |
---|---|
add |
선택적 요소입니다. 환경 변수 컬렉션에 환경 변수를 추가합니다. |
remove |
선택적 요소입니다. 환경 변수 컬렉션에서 환경 변수를 제거합니다. |
구성 샘플
다음 예제에서는 애플리케이션 풀에 환경 변수를 추가합니다.
<applicationPools>
<add name="Contoso" managedRuntimeVersion="v4.0" managedPipelineMode="Classic">
<environmentVariables>
<add name="foo" value="bar" />
</environmentVariables>
</add>
</applicationPools>
샘플 코드
다음 예제에서는 환경 변수 컬렉션에 이름/값 쌍을 추가하는 방법을 보여 줍니다.
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].environmentVariables.[name='foo',value='bar']" /commit:apphost
C#
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample {
private static void Main() {
using(ServerManager serverManager = new ServerManager()) {
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
ConfigurationElement addElement = FindElement(applicationPoolsCollection, "add", "name", @"Contoso");
if (addElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElementCollection environmentVariablesCollection = addElement.GetCollection("environmentVariables");
ConfigurationElement addElement1 = environmentVariablesCollection.CreateElement("add");
addElement1["name"] = @"foo";
addElement1["value"] = @"bar";
environmentVariablesCollection.Add(addElement1);
serverManager.CommitChanges();
}
}
private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
{
foreach (ConfigurationElement element in collection)
{
if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
{
bool matches = true;
for (int i = 0; i < keyValues.Length; i += 2)
{
object o = element.GetAttributeValue(keyValues[i]);
string value = null;
if (o != null)
{
value = o.ToString();
}
if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
{
matches = false;
break;
}
}
if (matches)
{
return element;
}
}
}
return null;
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Class Sample
Private Shared Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim applicationPoolsSection As ConfigurationSection = config.GetSection("system.applicationHost/applicationPools")
Dim applicationPoolsCollection As ConfigurationElementCollection = applicationPoolsSection.GetCollection
Dim addElement As ConfigurationElement = FindElement(applicationPoolsCollection, "add", "name", "Contoso")
If (addElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim environmentVariablesCollection As ConfigurationElementCollection = addElement.GetCollection("environmentVariables")
Dim addElement1 As ConfigurationElement = environmentVariablesCollection.CreateElement("add")
addElement1("name") = "foo"
addElement1("value") = "bar"
environmentVariablesCollection.Add(addElement1)
serverManager.CommitChanges
End Sub
Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
For Each element As ConfigurationElement In collection
If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
Dim matches As Boolean = True
Dim i As Integer
For i = 0 To keyValues.Length - 1 Step 2
Dim o As Object = element.GetAttributeValue(keyValues(i))
Dim value As String = Nothing
If (Not (o) Is Nothing) Then
value = o.ToString
End If
If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
matches = False
Exit For
End If
Next
If matches Then
Return element
End If
End If
Next
Return Nothing
End Function
End Class
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST");
var applicationPoolsCollection = applicationPoolsSection.Collection;
var addElementPos = FindElement(applicationPoolsCollection, "add", ["name", "Contoso"]);
if (addElementPos == -1) throw "Element not found!";
var addElement = applicationPoolsCollection.Item(addElementPos);
var environmentVariablesCollection = addElement.ChildElements.Item("environmentVariables").Collection;
var addElement1 = environmentVariablesCollection.CreateNewElement("add");
addElement1.Properties.Item("name").Value = "foo";
addElement1.Properties.Item("value").Value = "bar";
environmentVariablesCollection.AddElement(addElement1);
adminManager.CommitChanges();
function FindElement(collection, elementTagName, valuesToMatch) {
for (var i = 0; i < collection.Count; i++) {
var element = collection.Item(i);
if (element.Name == elementTagName) {
var matches = true;
for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
var property = element.GetPropertyByName(valuesToMatch[iVal]);
var value = property.Value;
if (value != null) {
value = value.toString();
}
if (value != valuesToMatch[iVal + 1]) {
matches = false;
break;
}
}
if (matches) {
return i;
}
}
}
return -1;
}
VBScript
Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST")
Set applicationPoolsCollection = applicationPoolsSection.Collection
addElementPos = FindElement(applicationPoolsCollection, "add", Array("name", "Contoso"))
If addElementPos = -1 Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set addElement = applicationPoolsCollection.Item(addElementPos)
Set environmentVariablesCollection = addElement.ChildElements.Item("environmentVariables").Collection
Set addElement1 = environmentVariablesCollection.CreateNewElement("add")
addElement1.Properties.Item("name").Value = "foo"
addElement1.Properties.Item("value").Value = "bar"
environmentVariablesCollection.AddElement(addElement1)
adminManager.CommitChanges()
Function FindElement(collection, elementTagName, valuesToMatch)
For i = 0 To CInt(collection.Count) - 1
Set element = collection.Item(i)
If element.Name = elementTagName Then
matches = True
For iVal = 0 To UBound(valuesToMatch) Step 2
Set property = element.GetPropertyByName(valuesToMatch(iVal))
value = property.Value
If Not IsNull(value) Then
value = CStr(value)
End If
If Not value = CStr(valuesToMatch(iVal + 1)) Then
matches = False
Exit For
End If
Next
If matches Then
Exit For
End If
End If
Next
If matches Then
FindElement = i
Else
FindElement = -1
End If
End Function