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

和自甴很熟 提交于 2021-02-11 13:21:29

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/65340545/azure-linux-app-service-with-net-core-stack-unable-to-use-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!