[英]Windows Azure: how do I expose a configuration setting as an environment variable?
I tried adding this to my ServiceDefinition.csdef file: 我尝试将其添加到我的ServiceDefinition.csdef文件中:
<WorkerRole ...><Runtime><Environment>
<Variable name="AZURE_STORAGE_ACCOUNT">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='AZURE_STORAGE_ACCOUNT']/@value" />
</Variable>
</Environment></Runtime></WorkerRole>
And I set the configuration setting in my ServiceConfiguration.Cloud.cscfg file: 然后在ServiceConfiguration.Cloud.cscfg文件中设置配置设置:
<Role name="WorkerRole">
<ConfigurationSettings>
<Setting name="AZURE_STORAGE_ACCOUNT" value="<secret stuff>" />
</ConfigurationSettings>
</Role>
But I got the following error when I run cspack
: 但是当我运行
cspack
时出现以下错误:
CloudServices091 : The '/RoleEnvironment/CurrentInstance/Configur
ationSettings/ConfigurationSetting[@name='AZURE_STORAGE_ACCOUNT']/@value' is an
invalid xpath expression.
Are you missing the declaration of that setting? 您是否缺少该设置的声明? I don't see the appropriate element in your
.csdef
, something like <ConfigurationSettings><Setting name="AZURE_STORAGE_ACCOUNT"/></ConfigurationSettings>
. 我没有在您的
.csdef
看到适当的元素,类似于<ConfigurationSettings><Setting name="AZURE_STORAGE_ACCOUNT"/></ConfigurationSettings>
。 You need one of those in your .csdef
, and then you still want the one in your .cscfg
that includes the value. 你需要那些在你的一个
.csdef
,然后你还是想在你的一个.cscfg
包含的价值。
If you're using Visual Studio, it should edit both files for you if you use its property view. 如果您使用的是Visual Studio,则使用其属性视图时,它应该为您编辑两个文件。 (Just double click the role and then click around to get to config settings and add a new one.)
(只需双击该角色,然后单击即可获得配置设置并添加一个新角色。)
The configuration seems to be correct. 配置似乎是正确的。 It would be better if you can make sure you're using the latest SDK.
如果您可以确保使用最新的SDK,那就更好了。 The xPath feature is available in Windows Azure SDK 1.5 and later.
Windows Azure SDK 1.5和更高版本中提供了xPath功能。
Best Regards, 最好的祝福,
Ming Xu. 徐明
I have tried out different options mentioned at blogs, like including the setting in both .cscfg and .csdef. 我尝试了博客中提到的其他选项,例如包括.cscfg和.csdef中的设置。 But, it doesn't seem to work.
但是,它似乎不起作用。 Also, other Xpath queries like
另外,其他Xpath查询
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/@id"/>
work correctly. 正常工作。
Finally, I figured out that the variable name used was different: 最后,我发现所使用的变量名称是不同的:
In cscfg I had : 在cscfg中,我有:
<Setting name="WFFEPeriodicRestartTime" value="168:00:00" />
in csdef I had : 在csdef中,我有:
<ConfigurationSettings>
<Setting name="PeriodicRestartTime" />
</ConfigurationSettings>
.... .... .... ....
<Variable name="PeriodicRestartTime">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='WFFEPeriodicRestartTime']/@value" />
</Variable>
Changed csdef to : 将csdef更改为:
<ConfigurationSettings>
<Setting name="WFFEPeriodicRestartTime" />
</ConfigurationSettings>
.... .... .... ....
<Variable name="WFFEPeriodicRestartTime">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='WFFEPeriodicRestartTime']/@value" />
</Variable>
It seems to work correctly now 现在看来可以正常工作
Ran into the same issue here as I was trying to read config values via the example provided here: https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-role-config-xpath#config-setting - lost quite a few brain cells in the process! 在这里遇到与我试图通过此处提供的示例读取配置值相同的问题: https : //docs.microsoft.com/zh-cn/azure/cloud-services/cloud-services-role-config-xpath#配置设置 -在此过程中丢失了很多脑细胞!
The trick is, this doesnt mean that you read config values from your web.config (which is how I mis read it), but from the Settings in the cscfg file. 诀窍是,这并不意味着您从web.config(这是我误读的方式)中读取配置值,而是从cscfg文件中的“设置”中读取。
With cloud services you have 1 csdef file that applies to all your cloud service convigurations, but then a cscfg for each environment. 使用云服务时,您有1个csdef文件适用于所有云服务,但每个环境都有一个cscfg。 In my scenario I have cscfg's for DEV, QA, Prod, and I needed to set a different variable depending on the environment.
在我的场景中,我有用于DEV,QA,Prod的cscfg,并且我需要根据环境设置不同的变量。
So for the OP's question, when you try to perform the "RoleEnvironment/CurrentInstance/ConfigurationSettings.." statement and get the invalid xpath value, its essentially saying "hey I cant find this value" 因此,对于OP的问题,当您尝试执行“ RoleEnvironment / CurrentInstance / ConfigurationSettings ..”语句并获取无效的xpath值时,其本质上是在说“嘿,我找不到此值”
How to make the magic happen: 如何使魔术发生:
Your csdef file needs the following: 您的csdef文件需要以下内容:
<ConfigurationSettings>
<Setting name="AZURE_STORAGE_ACCOUNT" />
</ConfigurationSettings>
Your cscfg then needs: 然后,您的cscfg需要:
<ConfigurationSettings>
<Setting name="AZURE_STORAGE_ACCOUNT" value="Some Secret Value" />
</ConfigurationSettings>
For my purposes I was trying to use this value in a startup.cmd file, so I was able to add the following to my csdef startup task 出于我的目的,我试图在startup.cmd文件中使用此值,因此我能够将以下内容添加到我的csdef启动任务中
<Startup>
<Task commandLine="Startup.cmd" executionContext="elevated" taskType="simple">
<Environment>
<Variable name="AZURE_STORAGE_ACCOUNT">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='AZURE_STORAGE_ACCOUNT']/@value" />
</Variable>
</Environment>
</Task>
</Startup>
Then in my startup.cmd you can just reference the value as %%AZURE_STORAGE_ACCOUNT%% 然后在我的startup.cmd中,您可以将值引用为%% AZURE_STORAGE_ACCOUNT %%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.