Running CLR/.NET code in-process with node.js

392 views
Skip to first unread message

Tomasz Janczuk

unread,
Feb 27, 2013, 3:29:41 PM2/27/13
to nodejs
Lately I have been scouting the idea of running .NET code in-process
within node.js applications. At this point I would like to cast a wide
net to ask for opinions and help in identifying additional use cases
where running .NET in node.js adds substantial value or solves a hard
problem.

The project was so far inspired by three main scenarios:

1. Implementing express.js request handlers and connect middleware for
node.js apps using .NET 4.5 and OWIN:
http://tomasz.janczuk.org/2013/02/hosting-net-code-in-nodejs-applications.html

2. Running CPU-bound workloads for node.js applications in-process
(without blocking the node.js event loop):
http://tomasz.janczuk.org/2013/02/cpu-bound-workers-for-nodejs.html

3. Accessing Windows-specific functionality from node.js applications
using C#/.NET instead of writing C/C++/Win32 node.js extensions (e.g.
access to SQL, ETW): http://tomasz.janczuk.org/2013/02/access-ms-sql-from-nodejs-application.html

The bits are at https://github.com/tjanczuk/owin.

I would love to hear opinions, ideas, suggestions, feedback etc. I
also do take contributions.

Thanks,
@tjanczuk

Jacob Groundwater

unread,
Feb 27, 2013, 4:51:32 PM2/27/13
to nod...@googlegroups.com
2. Running CPU-bound workloads for node.js applications in-process
(without blocking the node.js event loop):
http://tomasz.janczuk.org/2013/02/cpu-bound-workers-for-nodejs.html

Even in an application that supported threading, I would be unlikely to run CPU-bound operations in the same process as my HTTP server. Use a worker queue. 

Tomasz Janczuk

unread,
Feb 27, 2013, 5:05:17 PM2/27/13
to nodejs
I agree with this argument for long lasting CPU-bound operations (e.g.
weather prediction). How about CPU-bound operations that can be
reasonably completed within the lifetime of the original HTTP request?
It certainly saves a lot of orchestration to have another HTTP request
retrieve the results at a later time via short or long polling.

Jacob Groundwater

unread,
Feb 27, 2013, 5:12:41 PM2/27/13
to nod...@googlegroups.com
You can employ a work-queue in such a way that you don't close the original HTTP request. The HTTP server just hangs onto the connection until a reply is returned from the worker. Use something like ZeroMQ, or Axon to wire everything together. For any application that will undergo scaling, this is a very clean and logical place to separate responsibility.

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Tomasz Janczuk

unread,
Feb 27, 2013, 6:07:46 PM2/27/13
to nodejs
Currently you cannot choose the name of the method to call in .NET,
but you are not the first one asking, so I will be adding this
shortly.

You can have several CLR classes in one assembly and choose one this
way: https://github.com/tjanczuk/owin/issues/2

On Feb 27, 1:59 pm, mickael.metesr...@gmail.com wrote:
> Hi,
>
> I spent some time trying your project and I am very exciting about the
> connecting nodejs and .NET possibility.
>
> I focused on handling requests and process them with .NET 4.5 and I easily
> succeed to create simple REST application.
>
> First of all, is it possible to choose the name of the method calling by
> the connector (ie: MyMethod instead of Startup)?
>
> Then, can we connect several requests to different methods from the same
> assembly?
>
> Thanks,
>
> Mickael Metesreau

José F. Romaniello

unread,
Feb 27, 2013, 6:26:56 PM2/27/13
to nod...@googlegroups.com

Looks great, already told you on twitter.

There are cases where I would prefer this to an mq/worker solution.

A good example that I was thinking is a middleware to log events in windows event log. It is not a CPU bound operation but event log will be easy to use from .net. I already did a native module but this could be easier and better.

Glenn Block

unread,
Mar 3, 2013, 5:45:04 AM3/3/13
to nod...@googlegroups.com
I agree. For middleware that wants to take advantage of Windows it will be far easier to leverage .NET then to have to author C++ modules.

Actually I guess you could imagine the same for Mono.....

Glenn Block

unread,
Mar 3, 2013, 5:45:31 AM3/3/13
to nod...@googlegroups.com
I probably would _not_ use it for other than those specific cases.

Angel Java Lopez

unread,
Mar 3, 2013, 6:03:52 AM3/3/13
to nod...@googlegroups.com
And then, use ClearScript (JavaScript+V8) from .NET

End of Universe around the corner ;-)

Something we could need:

var dotnet = require('dotnet');

var form = dotnet.createNew('System.Windows.Forms');
form.setText('Look ma... WinForm from Node.js');
form.show();

(Ok, I missed the Application loop)

That is, some module that expose (via reflection) the native methods
on an object or type to JavaScript/V8.

I don't know:

- How to manage garbage collection (V8 objects vs .NET objects)
- How to "marshall" arguments/return values (V8 objects vs .NET
objects) and if it is feasible
- V8 calls C++. How to call .NET from C++.

Any example of how to return a new V8 object in C++ with C++ methods
attached? Like the above form.setText?

Maybe, @tomasz code has some answers to me. But V8 C++ code is still
some obscure black magic to me, yet.

Some collected links to explore:

http://www.codeproject.com/Articles/42319/Using-NET-Classes-Modules-from-Native-C
http://stackoverflow.com/questions/380559/using-net-class-from-native-c-using-c-cli-as-a-middleware
http://msdn.microsoft.com/en-us/magazine/cc300632.aspx
http://weblogs.asp.net/kennykerr/archive/2005/07/12/Mixing-Native-and-Managed-Types-in-C_2B002B00_.aspx

Angel "Java" Lopez
@ajlopez

Glenn Block

unread,
Mar 3, 2013, 12:37:05 PM3/3/13
to nod...@googlegroups.com
lol. OWIN is a pipeline driven thing which works really well for request/response. I think popping up a windows form

1. Might not really fit in.
2. Would have catastrophic effects like the end of the universe as we know it. 

:-)
Reply all
Reply to author
Forward
0 new messages