简体   繁体   中英

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. I did this a while ago on a Windows Service Plan, there it was working. Now I am trying with a Linux Plan and it is not working.

First I was trying to use "Jering.Javascript.NodeJS" and then also "INodeServices" from Microsoft (which is obsolete). 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. 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.

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. 在此处输入图像描述

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. Azure Portal Screenshot

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.

Linux Service Plans runs on Oryx which is documented here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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