繁体   English   中英

无法从Node.js读取Azure配置设置

[英]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模拟器内部或云中以辅助角色运行时 ,这些命令才有效

这是我的解决方案。 这不好,但至少它现在有效。

  1. 像这样创建一个.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; } } 

    }

  2. 启动任务以读取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 &gt; ..\.env executionContext="elevated" />
  1. 在NodeJS Web角色中,我们只需要通过process.env["YOUR_SETTING_KEY"]读取此变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM