[英]Can't read Azure configuration settings from Node.js
我在Azure中運行Node.js應用程序,並嘗試獲取如下配置設置:
var azure = require('azure');
azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
if (error) {
console.log(error);
return;
}
console.log('Settings: %s', settings);
});
但輸出始終如下:
{ "code": "ENOENT", "errno": "ENOENT", "syscall": "connect", "fileName": "\\\\.\\pipe\\WindowsAzureRuntime" }
我使用IISNode使用所有最新位在IIS中運行Node.Js. 如果我在Azure VM上手動運行節點(即node.exe server.js),我也會收到同樣的錯誤。 在Azure Development Fabric中運行我的PC時也一樣。
在此先感謝您的任何幫助或建議!
由於您提到您在IISNode中運行,因此它必須是Web角色。 請注意SDK自述文件中的以下內容:
Service Runtime允許您與運行當前角色的計算機環境進行交互。 請注意, 只有當您的代碼在Azure模擬器內部或雲中以輔助角色運行時 ,這些命令才有效 。
這是我的解決方案。 這不好,但至少它現在有效。
像這樣創建一個.NET控制台應用程序
使用Microsoft.WindowsAzure; 使用系統; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.Threading; 使用System.Threading.Tasks;
namespace CloudConfigurationHelper {class Program {static int MaxRetryTime = 10;
public static Dictionary<string, string> GetSettings(string[] keys) { Dictionary<string, string> settings = new Dictionary<string, string>(); try { foreach (string key in keys) { settings[key] = CloudConfigurationManager.GetSetting(key); } } catch { MaxRetryTime--; if (MaxRetryTime <= 0) { return settings; } Thread.Sleep(2000); return GetSettings(keys); } return settings; } static int Main(string[] args) { var settings = GetSettings(args); if (settings.Count != args.Length) { return -1; } foreach (string key in settings.Keys) { Console.WriteLine(key + "=" + settings[key]); } return 0; } }
}
啟動任務以讀取azure配置變量並寫入.env文件。 使用https://github.com/motdotla/dotenv讀取.env文件並加載到process.env。
Env.cmd文件:
@echo off
cd %~dp0
CloudConfigurationHelper.exe %*
將啟動任務添加到ServiceDefinition.csdef:
<Task commandLine="Env.cmd YOUR_SETTING_KEY > ..\.env executionContext="elevated" />
process.env["YOUR_SETTING_KEY"]
讀取此變量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.