简体   繁体   English

Azure Linux 带有 .Net Core Stack 的应用服务。 无法使用 NodeJS

[英]Azure Linux App Service with .Net Core Stack. Unable to use NodeJS

I am hosting a .NET Core Application on MS Azure (on a Linux Service Plan) and I want to run some NodeJS code in the .NET Core Application.我在 MS Azure 上托管一个 .NET 核心应用程序(在 Linux 服务计划上),我想在 .NET 核心应用程序中运行一些 NodeJS 代码。 I did this a while ago on a Windows Service Plan, there it was working.我不久前在 Windows 服务计划上做了这个,它在那里工作。 Now I am trying with a Linux Plan and it is not working.现在我正在尝试使用 Linux 计划,但它不起作用。

First I was trying to use "Jering.Javascript.NodeJS" and then also "INodeServices" from Microsoft (which is obsolete).首先,我尝试使用“Jering.Javascript.NodeJS”,然后还尝试使用 Microsoft 的“INodeServices”(已过时)。 But "node" was not found.但是没有找到“节点”。 I also tried to start directly a Process (Code below), but also not working.我也尝试直接启动一个进程(下面的代码),但也没有用。 "node" is not found.找不到“节点”。

            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 is installed on the server and also the command works there but it seems that the .NET Core app is hosted as docker and does not have any access outside to run NodeJS. NodeJS 安装在服务器上,命令也在那里工作,但似乎 .NET Core 应用程序托管为 docker 并且没有任何外部访问权限来运行 NodeJS。 Image图片

I found a useful information here .我在这里找到了有用的信息。

The problem is that Node is not present in the container so it is necessary to have a script to install and start it before starting the app itself.问题是容器中不存在 Node,因此在启动应用程序本身之前,必须有一个脚本来安装和启动它。

Reproduce:复制: 在此处输入图像描述

Here is my script:这是我的脚本:

//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;

It works on my .net Core app based on Linux.它适用于我的基于 Linux 的 .net 核心应用程序。 在此处输入图像描述

I think I found a better solution;) In an app service you can mount a storage.我想我找到了更好的解决方案;)在应用服务中,您可以安装存储。 In my case I mounted a storage, which contains the nodeJS lib.就我而言,我安装了一个存储,其中包含 nodeJS 库。 Azure Portal Screenshot Azure传送门截图

Now i can execute the following code:现在我可以执行以下代码:

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();

You can create on azure portal an environment var named POST_BUILD_COMMAND with a command to fix your environment path.您可以在 azure 门户上创建一个名为POST_BUILD_COMMAND的环境变量,并使用命令修复您的环境路径。

Linux Service Plans runs on Oryx which is documented here Linux 服务计划在此处记录的 Oryx 上运行

POST_BUILD_COMMAND=PATH=/usr/bin/node:$PATH

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

相关问题 无法在 Azure 应用服务 Linux 中部署 NodeJS(Fastify 应用) - Unable to deploy NodeJS (Fastify app) in Azure App Service Linux 使用ASP.NET Core NodeServices的Azure应用服务上的NodeJ不会执行 - NodeJs on Azure app service using ASP.NET Core NodeServices does not execute 无法在 Azure 应用服务上的 NodeJS 容器上设置环境变量 - Unable to set the environment variables on a NodeJS Container on Azure App Service 在Oracle Linux中为Node.js应用程序创建服务 - Creating service for nodejs app in Oracle linux Azure 应用服务 - Nodejs ES 模块错误 - Azure App Service - Nodejs ES Module Error Azure 应用服务上的多个 NodeJS 实例 - Multiple NodeJS instance on Azure App Service 无法将 Azure 应用服务中的 NodeJS 应用连接到 Azure MySQL 数据库 - Unable to connect NodeJS app in Azure App Services to Azure MySQL Database 使用 NodeJS Express + Azure 应用服务(.NET 运行时)时,较大的请求会冻结 - Larger requests freeze when using NodeJS Express + Azure App service (.NET runtime) 将 NodeJS 应用程序连接到 SignalR(使用 .NET Core 3) - Connecting NodeJS app to SignalR (with .NET Core 3) 我可以在同一个应用程序中为 PHP 和 NodeJs 使用 Azure web 应用程序服务吗? - Can I use the Azure web app service for PHP and NodeJs both in the same application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM