Tomasz Janczuk of Microsoft posted about owin, .NET and node.js. Here is the project on github.
The owin project allows hosting .NET 4.5 code in node.js applications running on Windows.
You need Windows x64 with node.js 0.8.x x64 installed (the module had been developed against node.js 0.8.19). You also need .NET Framework 4.5 on the machine.
PowerShell + node.js
Below is the PowerShell script at work.
The owin module allows running CPU-bound computations implemented in .NET in-process with the node.js application without blocking the node.js event loop.
The PowerShell script and C# implementation
The script has three steps:
- Compiles the C# which implements calling PowerShell as a Owinjs.Worker. This first step compiles to a DLL the C# implementing the PowerShell call simulating a CPU-bound computation. Simply, it calculates income by subtracting the cost from the revenue passed in from the JavaScript and does a Start-Sleep 5 to become CPU-bound.
- Next it takes the JavaScript, does a Set-Content to a file test.js.
- Finally, we invoke node on the test.js file created in step 2
Note: You also need the Owinjs.dll in the same directory as the script to make this happen. Download the zip file at the end of the post.
$ReferencedAssemblies = "$pwd\Owinjs.dll" $powerShell = @" using System.Collections.Generic; using System.Management.Automation; namespace CalculateBudget { public class Startup : Owinjs.Worker { protected override IDictionaryExecute(IDictionary Add-Type -ReferencedAssemblies $ReferencedAssemblies ` -OutputAssembly CalculateBudget.dll ` -TypeDefinition $powerShell Write-Host -fore green "Compiled DLL" if($?) { @" var owin = require('owin') console.log('Starting long running operation...'); owin.worker( 'CalculateBudget.dll', { revenue: 100, cost: 80 }, function (error, result) { if (error) throw error; console.log('Result of long running operation: ', result); } ); setInterval(function () { console.log('Node.js event loop is alive!') }, 1000); "@ | Set-Content -Encoding Ascii .\test.js Write-Host -fore green "Create JavaScript file" Write-Host -fore green "Running nodejs" node .\test.js }input) { var script = string.Format(@"{0}-{1} ; Start-Sleep 5", input["revenue"], input["cost"]); var income = PowerShell .Create() .AddScript(script) .Invoke()[0] .ToString(); return new Dictionary { { "income", income } }; } } } "@
This opens interesting ways to experiment with Node, .NET and PowerShell.
Download
Here it is.
Owinjs.zip
Enjoy!





{ 1 trackback }
{ 1 comment… read it below or add one }
Wow, now really one can become the polyglot master of ‘The Stack’