Edge.js - Running Node.js and .NET in One Process

C# is the most powerful static type programming language which can be used for developing wide variety of applications. Node.js is a server-side JavaScript platform which can be used for building high performance scalable applications on the V8 JavaScript engine. .NET provides more than 11,000 packages via NuGet and Node.js provides more than 30,000 modules via NPM. It would be great if we could use both .NET and Node.js platforms in one place. Edge.js is a Node.js npm module that lets the developers to run .NET and Node.js code in one process, where you can call  .NET functions from Node.js and Node.js functions from .NET. With Edge.js, you can call C# async lambda from node.js. Both .NET and Node.js are using different threading models, but Edge does handle the threading models of single threaded V8 and multi-threaded CLR. Currently Edge.js supports to use JavaScript with C#, F#, Python, and PowerShell. Edge.js was developed by Tomasz Janczuk. The Edge.js framework hosted on Github at https://github.com/tjanczuk/edge

The following components required for working with Edge.js framework:

Working with Edge.js

You first install to Edge.js using npm in order to working Edge.js framework

image

Calling C# class from Node.js

The code block below provides a sample Node.js program which is using C# code for converting images from one image format to another image format

   1:  var edge = require('edge');
   2:  var convertImage = edge.func(function() {/*
   3:  #r "System.Drawing.dll"
   4:  
   5:   using System;
   6:   using System.Threading.Tasks;
   7:   using System.Collections.Generic;
   8:   using System.Drawing;
   9:   using System.Drawing.Imaging;
  10:  
  11:   class Startup
  12:  {
  13:    static IDictionary<string, ImageFormat> formats
  14:              = new Dictionary<string, ImageFormat> 
  15:          {
  16:              { "jpg", ImageFormat.Jpeg },
  17:              { "bmp", ImageFormat.Bmp },
  18:              { "gif", ImageFormat.Gif },
  19:              { "tiff", ImageFormat.Tiff },
  20:              { "png", ImageFormat.Png }
  21:          };
  22:  
  23:  public async Task<object> Invoke(
  24:                  IDictionary<string,object> input)
  25:   {
  26:         await Task.Run(async () => {
  27:            using (Image image = Image.FromFile(
  28:                    (string)input["source"]))
  29:     {
  30:            image.Save((string)input["destination"], 
  31:                   formats[(string)input["toType"]]);
  32:         }
  33:     });
  34:  
  35:              return null;
  36:          }
  37:      }
  38:  */});
  39:   
  40:  var params = { 
  41:      source: '.\\shiju.png', 
  42:      destination: '.\\shiju.jpg',
  43:      toType: 'jpg'
  44:  };
  45:   
  46:  convertImage(params, function (error) {
  47:      if (error) throw error;
  48: console.log('The image shiju.png has been asynchronously converted to shiju.jpg');
  49:  });

In the above code block, we are using the C# code using the edge.func where you can put C# async lambdas. In the above code block, we are using a C# class, which can be specified using the class name Startup with an Invoke method that matches the Func<object,Task<object>> delegate signature. We can add reference to the .Net assemblies using #r. The above program will convert the image to jpg format from png format where conversion logic was written in C# and called it from Node.js

You can run the above program as a normal node.js program as shown in the below command prompt:

image

 

 

3 Comments

Comments have been disabled for this content.