Introduction

This was and still is the real appeal of technologies like Node.js, however, it's quite easy to do within the realm of .NET. This post is going to provide a very basic overview of how to target and execute some arbitrary JavaScript code within .NET without ever touching the browser.
What is/are NodeServices?
I'll let the creator, Steve Sanderson, explain it as follows:
- NodeServices provides a fast and robust way for .NET code to run JavaScript on the server inside a Node.js environment. You can use this to consume arbitrary functionality from NPM packages at runtime in your ASP.NET Core app.
- Most applications developers don't need to use this directly, but you can do so if you want to implement your own functionality that involves calling Node.js code from .NET at runtime.
You can find the project itself on GitHub as well, which goes far beyond just interacting with just Node in .NET.
Setting Up NodeServices
This particular example of NodeServices depends on .NET Core 2.0, which you will want to install from here if you want to follow along. The steps may work with earlier versions, but if you run into issues, consider trying 2.0.
In this example, we will build a simple Web API that will rely on NodeServices to perform some operations. So, to get started, we'll first need to create a new project:
dotnet new webapi
If you aren't a command-line person, Visual Studio 2017 Update 3 should have the necessary tools to create .NET Core 2.0 applications through the traditional GUI.
Configuring NodeServices
Getting started with NodeServices is dead simple. You basically just need to include the Microsoft.AspNetCore.NodeServices NuGet package within your application via the following command:
dotnet add package Microsoft.AspNetCore.NodeServices
You should then see it in your project definition as well,
- <ItemGroup>
- <!-- Others omitted for brevity -->
- <PackageReference Include="Microsoft.AspNetCore.NodeServices" Version="2.0.0" />
- </ItemGroup>
Then, you'll need to configure the necessary middleware to handle using the service within your application in the ConfigureServices() method of your Startup.cs file,
- public void ConfigureServices(IServiceCollection services)
- {
- // This will configure the NodeServices service
- services.AddNodeServices();
- }
After configuring the services, you'll now be able to use dependency-injection to handle injecting this into your application at the controller level,
- public class ValuesController : Controller
- {
- private readonly INodeServices _nodeServices;
- public ExampleController(INodeServices nodeServices)
- {
- _nodeServices = nodeServices;
- }
- // Other actions here
- }
Or at a single method/action level with the [FromServices] attribute,
- public async Task<IActionResult> Example([FromServices] INodeServices nodeServices)
- {
- // TODO: Cool stuff
- }
Now, that we have the actual Node Services wired up, let's look at what has to be done on the JavaScript side of things get the two sides to play nicely with one another.
Actually Using NodeServices
Since we are going to be calling JavaScript code from within .NET, we will first need to define the actual code that we want to call. So to get started, we will create a new Scripts folder at the root of the current project and a new file called Add.js within it.
This Add.js file is going to function as a single module that will export a function that will be called by our C# code. For the sake of simplicity, we will start by simply adding two numbers together and returning the result via a callback,

- module.exports = function(a, b, callback) {
- let result = a + b;
- callback(result);
- };
Jumping back to the .NET side of things, we can configure our service to point to our new Javascript file using the InvokeAsync<T>() method, which expects the path to the file being invoked and a type to indicate the expected return type,




Anisha PareekPosted Feb 3, 2022, 1:48 PM
Does it works after published as well... Because after publishing inodeservice not able to find the js file.... I have worked with js and ts file
mark goldinPosted Jan 18, 2021, 3:53 PM
Microsoft.AspNetCore.NodeServices has been obsolete. What do we use now?
Nigel FernandesPosted Nov 1, 2017, 6:29 PM
Nice, thats awesome
Naveen BishtPosted Oct 23, 2017, 4:14 AM
Cool...really love this ...