How to Use Node Services in ASP.NET Core

You can use easily use Node.js in your ASP.NET Core application with Microsoft.AspNetCore.NodeServices package. Add the Node Services middleware to the request pipeline. You can do it in your ConfigureServices() method.


       //Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
            services.Configure<CookiePolicyOptions>(options =>
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;


Now you’re able to get instances of INodeServices in your application. INodeServices is the API through which .NET code can make calls into JavaScript that runs in a Node environment. You can use the FromServices attribute to get the instance of `INodeServices’ in your action method. Here is Add method implementation in MVC.


        public async Task<IActionResult> Add([FromServices] INodeServices nodeServices)
            var num1 = 10;
            var num2 = 20;
            var result = await nodeServices.InvokeAsync<int>("AddModule.js", num1, num2);
            ViewData["ResultFromNode"] = $"Result of {num1} + {num2} is {result}";
            return View();

And here is the code of the AddModule.js file.


module.exports = function(callback, num1, num2) { 
  var result = num1 + num2;
  callback(null, result); 
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

SQL server- network related or instance-specific error happened while connection establishing

  Instance-specific or network related error happening while connecting to SQL server   Please...

OleDB connection string examples

OleDB connection string examples MS Access (Jet) "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA...

Do you allow custom COM components?

Do you allow custom COM components? We are not offering to install the custom COM components...

Error when accessing a WCF services

Error when accessing a WCF services Error when accessing a WCF service: "IIS specified...

How to cache static contents to client with UseMaxAge?

You can consider to caches static contents to the client with UseMaxAge if your website has too...