重要
从 2024 年 9 月 1 日开始,云服务(经典版)对所有客户已停止支持。 从 2024 年 10 月开始,Microsoft 将停止并关闭任何正在运行的现有部署,数据将永久性丢失。 新部署应使用新的基于 Azure 资源管理器的部署模型 Azure 云服务(扩展支持)。
在云服务辅助角色或 Web 角色服务定义文件中,可以将运行时配置值公开为环境变量。 支持以下 XPath 值(分别对应于 API 值)。
这些 XPath 值也可通过 Microsoft.WindowsAzure.ServiceRuntime 库使用。
应用在模拟器中运行
指示应用正在模拟器中运行。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/Deployment/@emulated" |
| 代码 | var x = RoleEnvironment.IsEmulated; |
部署识别号
检索实例的部署 ID。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/Deployment/@id" |
| 代码 | var deploymentId = RoleEnvironment.DeploymentId; |
角色标识
检索实例的当前角色 ID。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/@id" |
| 代码 | var id = RoleEnvironment.CurrentRoleInstance.Id; |
更新域名
检索实例的更新域。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/@updateDomain" |
| 代码 | var ud = RoleEnvironment.CurrentRoleInstance.UpdateDomain; |
故障域
检索实例的故障域。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/@faultDomain" |
| 代码 | var fd = RoleEnvironment.CurrentRoleInstance.FaultDomain; |
角色名称
检索实例的角色名称。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/@roleName" |
| 代码 | var rname = RoleEnvironment.CurrentRoleInstance.Role.Name; |
配置设置
检索指定配置设置的值。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Setting1']/@value" |
| 代码 | var setting = RoleEnvironment.GetConfigurationSettingValue(“Setting1”); |
本地存储路径
检索实例的本地存储路径。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@path" |
| 代码 | var localResourcePath = RoleEnvironment.GetLocalResource(“LocalStore1”).RootPath; |
本地存储大小
检索实例的本地存储大小。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@sizeInMB" |
| 代码 | var localResourceSizeInMB = RoleEnvironment.GetLocalResource("LocalStore1").MaximumSizeInMegabytes; |
终结点协议
检索实例的终结点协议。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@protocol" |
| 代码 | var prot = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].Protocol; |
端点 IP
获取指定终结点的 IP 地址。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@address" |
| 代码 | var address = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Address // 获取“Endpoint1”的IP地址 |
终结点端口
检索实例的终结点端口。
| 类型 | 示例 |
|---|---|
| XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@port" |
| 代码 | var port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Port; |
示例
这里是一个辅助角色的示例,该角色通过一个名为 TestIsEmulated 的环境变量创建了一个启动任务,并将其设置为 @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>
后续步骤
详细了解 ServiceConfiguration.cscfg 文件。
创建 ServicePackage.cspkg 包。
为角色启用 远程桌面 。