使用 XPath 将角色配置设置公共为环境变量Expose role configuration settings as an environment variable with XPath

在云服务辅助角色或 Web 角色服务定义文件中,可以将运行时配置值公开为环境变量。In the cloud service worker or web role service definition file, you can expose runtime configuration values as environment variables. 支持以下 XPath 值(分别对应于 API 值)。The following XPath values are supported (which correspond to API values).

Microsoft.WindowsAzure.ServiceRuntime 库中也提供了这些 XPath 值。These XPath values are also available through the Microsoft.WindowsAzure.ServiceRuntime library.

应用在模拟器中运行App running in emulator

指示应用正在模拟器中运行。Indicates that the app is running in the emulator.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/Deployment/@emulated"xpath="/RoleEnvironment/Deployment/@emulated"
代码Code var x = RoleEnvironment.IsEmulated;var x = RoleEnvironment.IsEmulated;

部署 IDDeployment ID

检索实例的部署 ID。Retrieves the deployment ID for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/Deployment/@id"xpath="/RoleEnvironment/Deployment/@id"
代码Code var deploymentId = RoleEnvironment.DeploymentId;var deploymentId = RoleEnvironment.DeploymentId;

角色 IDRole ID

检索实例的当前角色 ID。Retrieves the current role ID for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/@id"xpath="/RoleEnvironment/CurrentInstance/@id"
代码Code var id = RoleEnvironment.CurrentRoleInstance.Id;var id = RoleEnvironment.CurrentRoleInstance.Id;

更新域Update domain

检索实例的更新域。Retrieves the update domain of the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/@updateDomain"xpath="/RoleEnvironment/CurrentInstance/@updateDomain"
代码Code var ud = RoleEnvironment.CurrentRoleInstance.UpdateDomain;var ud = RoleEnvironment.CurrentRoleInstance.UpdateDomain;

容错域Fault domain

检索实例的容错域。Retrieves the fault domain of the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/@faultDomain"xpath="/RoleEnvironment/CurrentInstance/@faultDomain"
代码Code var fd = RoleEnvironment.CurrentRoleInstance.FaultDomain;var fd = RoleEnvironment.CurrentRoleInstance.FaultDomain;

角色名称Role name

检索实例的角色名称。Retrieves the role name of the instances.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/@roleName"xpath="/RoleEnvironment/CurrentInstance/@roleName"
代码Code var rname = RoleEnvironment.CurrentRoleInstance.Role.Name;var rname = RoleEnvironment.CurrentRoleInstance.Role.Name;

配置设置Config setting

检索指定配置设置的值。Retrieves the value of the specified configuration setting.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Setting1']/@value"xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Setting1']/@value"
代码Code var setting = RoleEnvironment.GetConfigurationSettingValue("Setting1");var setting = RoleEnvironment.GetConfigurationSettingValue("Setting1");

本地存储路径Local storage path

检索实例的本地存储路径。Retrieves the local storage path for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@path"xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@path"
代码Code var localResourcePath = RoleEnvironment.GetLocalResource("LocalStore1").RootPath;var localResourcePath = RoleEnvironment.GetLocalResource("LocalStore1").RootPath;

本地存储大小Local storage size

检索实例的本地存储大小。Retrieves the size of the local storage for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@sizeInMB"xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@sizeInMB"
代码Code var localResourceSizeInMB = RoleEnvironment.GetLocalResource("LocalStore1").MaximumSizeInMegabytes;var localResourceSizeInMB = RoleEnvironment.GetLocalResource("LocalStore1").MaximumSizeInMegabytes;

终结点协议Endpoint protocol

检索实例的终结点协议。Retrieves the endpoint protocol for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@protocol"xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@protocol"
代码Code var prot = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].Protocol;var prot = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].Protocol;

终结点 IPEndpoint IP

获取指定终结点的 IP 地址。Gets the specified endpoint's IP address.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@address"xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@address"
代码Code var address = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Addressvar address = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Address

终结点端口Endpoint port

检索实例的终结点端口。Retrieves the endpoint port for the instance.

类型Type 示例Example
XPathXPath xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@port"xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@port"
代码Code var port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Port;var port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Port;

示例Example

下面是使用名为 TestIsEmulated、设置为 @emulated xpath value 的环境变量创建启动任务的辅助角色示例。Here is an example of a worker role that creates a startup task with an environment variable named TestIsEmulated set to the @emulated xpath value.

<WorkerRole name="Role1">
    <ConfigurationSettings>
      <Setting name="Setting1" />
    </ConfigurationSettings>
    <LocalResources>
      <LocalStorage name="LocalStore1" sizeInMB="1024"/>
    </LocalResources>
    <Endpoints>
      <InternalEndpoint name="Endpoint1" protocol="tcp" />
    </Endpoints>
    <Startup>
      <Task commandLine="example.cmd inputParm">
        <Environment>
          <Variable name="TestConstant" value="Constant"/>
          <Variable name="TestEmptyValue" value=""/>
          <Variable name="TestIsEmulated">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated"/>
          </Variable>
          ...
        </Environment>
      </Task>
    </Startup>
    <Runtime>
      <Environment>
        <Variable name="TestConstant" value="Constant"/>
        <Variable name="TestEmptyValue" value=""/>
        <Variable name="TestIsEmulated">
          <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated"/>
        </Variable>
        ...
      </Environment>
    </Runtime>
    ...
</WorkerRole>

后续步骤Next steps

了解有关 ServiceConfiguration.cscfg 文件的详细信息。Learn more about the ServiceConfiguration.cscfg file.

创建 ServicePackage.cspkg 包。Create a ServicePackage.cspkg package.

为角色启用 远程桌面Enable remote desktop for a role.