【问题标题】:Azure Linux App Service with .Net Core Stack. Unable to use NodeJS带有 .Net Core Stack 的 Azure Linux 应用服务。无法使用 Node.js
【发布时间】:2021-03-28 03:13:07
【问题描述】:

我在 MS Azure 上托管一个 .NET Core 应用程序(在 Linux 服务计划上),我想在 .NET Core 应用程序中运行一些 NodeJS 代码。不久前,我在 Windows 服务计划中这样做了,它在那里工作。现在我正在尝试使用 Linux 计划,但它不起作用。

首先我尝试使用“Jering.Javascript.NodeJS”,然后还尝试使用 Microsoft 的“INodeServices”(已过时)。但是没有找到“节点”。我也尝试直接启动一个进程(下面的代码),但也没有工作。未找到“节点”。

            var proc = new System.Diagnostics.Process
            {
                StartInfo = new System.Diagnostics.ProcessStartInfo
                {
                    FileName = "node",
                    Arguments = " -v",
                    RedirectStandardOutput = true
                }
            };
            result += "RUN: " + proc.StartInfo.FileName;
            proc.Start();
            var reader = proc.StandardOutput;

NodeJS 安装在服务器上,并且该命令在那里工作,但似乎 .NET Core 应用程序作为 docker 托管,并且没有任何外部访问权限来运行 NodeJS。 Image

【问题讨论】:

    标签: node.js linux .net-core azure-web-app-service


    【解决方案1】:

    我找到了有用的信息here

    问题是容器中不存在节点,所以它是 在启动之前需要有一个脚本来安装和启动它 应用程序本身。

    转载:

    这是我的脚本:

    //using System.Diagnostics;
    ProcessStartInfo startinfo = new ProcessStartInfo();
    startinfo.FileName = "bash";
    //startinfo.FileName = "/etc/opt/nodejs/14.15.0/bin/node"; //it's no use even node package located here.
    Process process = new Process();
    process.StartInfo = startinfo;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    //install and start nodejs
    process.StandardInput.WriteLine("apt-get install curl");
    process.StandardInput.WriteLine("curl -sL https://deb.nodesource.com/setup_12.x | bash");
    process.StandardInput.WriteLine("apt-get install -y nodejs");
    //Run "node -v"
    process.StandardInput.WriteLine("node -v");
    string line = string.Empty;
            
    while (!process.StandardOutput.EndOfStream)
    {
         line = process.StandardOutput.ReadLine();
         _logger.LogInformation(line);
    }
    process.WaitForExit();
    return string.Empty;
    

    它适用于我基于 Linux 的 .net Core 应用程序。

    【讨论】:

    • 您是否直接在应用程序中运行此脚本?这样在每次启动时都会执行脚本,还是在 azure 门户中有设置?
    • Portal 上没有其他设置,我只是直接在我的应用程序中运行脚本。 @MichaelEckhart-Wöllkart
    • 感谢您的快速响应。但这意味着应用的启动时间增加了很多,对吧?
    • 是的,它会导致启动时间增加...但不会太多。@MichaelEckhart-Wöllkart
    • 谢谢,也许我找到了更好的解决方案,如果你愿意,可以看看我的回答。
    【解决方案2】:

    我想我找到了更好的解决方案;) 在应用服务中,您可以挂载存储。就我而言,我安装了一个存储,其中包含 nodeJS 库。 Azure Portal Screenshot

    现在我可以执行以下代码:

    string result = "";
    var proc = new System.Diagnostics.Process
    {
        StartInfo = new System.Diagnostics.ProcessStartInfo
        {
            FileName = "/externallibs/node/bin/node",
            Arguments = " -v",
            RedirectStandardOutput = true
        }
    };
    result += "RUN: " + proc.StartInfo.FileName;
    proc.Start();
    var reader = proc.StandardOutput;
    return result +  reader.ReadToEnd();
    

    【讨论】:

    • 太棒了!感谢分享,迈克尔。
    • 不客气 ;) 如果你也添加类似的东西,你也可以只使用“节点”而不必使用整个路径。 "Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + @":/externallibs/node/bin");"
    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 2020-10-26
    • 2018-08-15
    • 1970-01-01
    • 2020-11-03
    • 2018-01-23
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多