在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tjanczuk/edge开源软件地址(OpenSource Url):https://github.com/tjanczuk/edge开源编程语言(OpenSource Language):C++ 77.7%开源软件介绍(OpenSource Introduction):Edge.js: .NET and Node.js in-processNEW Edge.js is now on Slack at https://edgejs.slack.com. Join here. An edge connects two nodes. This edge connects Node.js and .NET. V8 and CLR/.NET Core/Mono - in process. On Windows, MacOS, and Linux. You can script C# from a Node.js process: ES5 var edge = require('edge');
var helloWorld = edge.func(function () {/*
async (input) => {
return ".NET Welcomes " + input.ToString();
}
*/});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
}); ES6 In ES6 you can use template strings to write multiline C# code. var edge = require('edge');
var helloWorld = edge.func(`
async (input) => {
return ".NET Welcomes " + input.ToString();
}
`);
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
}); You can also script Node.js from C#: using System;
using System.Threading.Tasks;
using EdgeJs;
class Program
{
public static async Task Start()
{
var func = Edge.Func(@"
return function (data, callback) {
callback(null, 'Node.js welcomes ' + data);
}
");
Console.WriteLine(await func(".NET"));
}
static void Main(string[] args)
{
Start().Wait();
}
} What problems does Edge.js solve?
--Scott Hanselman (@shanselman) Before you dive inSee the Edge.js overview. ContentsIntroduction IntroductionEdge.js allows you to run Node.js and .NET code in one process on Windows, MacOS, and Linux. You can call .NET functions from Node.js and Node.js functions from .NET. Edge.js takes care of marshalling data between CLR and V8. Edge.js also reconciles threading models of single threaded V8 and multi-threaded CLR. Edge.js ensures correct lifetime of objects on V8 and CLR heaps. The CLR code can be pre-compiled or specified as C#, F#, Python, or PowerShell source: Edge.js can compile CLR scripts at runtime. Edge can be extended to support other CLR languages or DSLs. Edge.js provides an asynchronous, in-process mechanism for interoperability between Node.js and .NET. You can use this mechanism to:
Read more about the background and motivations of the project here. Follow @tjanczuk for updates related to the module. Scripting CLR from Node.jsIf you are writing a Node.js application, this section explains how you include and run CLR code in your app. It works on Windows, MacOS, and Linux. What you needEdge.js runs on Windows, Linux, and OSX and requires Node.js 8.x, 7.x, 6.x, as well as .NET Framework 4.5 (Windows), Mono 4.2.4 (OSX, Linux), or .NET Core 1.0.0 Preview 2 (Windows, OSX, Linux). NOTE there is a known issue with Mono after 4.2.4 that will be addressed in Mono 4.6. Windows
If you have both desktop CLR and .NET Core installed, read using .NET Core for how to configure Edge to use one or the other. Linux
OSX
DockerEdge.js is available as a Docker image on the tjanczuk/edgejs repository on Docker Hub. The image is based on Debian Trusty, and contains Node.js 6.3.0 x64, Mono 4.2.4 x64, .NET Core 1.0.0 Preview 2 x64 (dotnet-dev-1.0.0-preview2-003121), and Edge.js 6.5.1: By default Edge uses Mono to execute CLR code:
Specify the
Alternatively, you can also specify
How to: C# hello, worldFollow setup instructions for your platform. Install edge:
In your server.js: var edge = require('edge');
var helloWorld = edge.func(function () {/*
async (input) => {
return ".NET Welcomes " + input.ToString();
}
*/});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
}); Run and enjoy:
If you want to use .NET Core as your runtime and are running in a dual runtime environment (i.e. Windows with .NET 4.5 installed as well or Linux with Mono installed), you will need to tell edge to use .NET Core by setting the
How to: integrate C# code into Node.js codeEdge provides several ways to integrate C# code into a Node.js application. Regardless of the way you choose, the entry point into the .NET code is normalized to a Edge provides a function that accepts a reference to C# code in one of the supported representations, and returns a Node.js function which acts as a JavaScript proxy to the var edge = require('edge');
var myFunction = edge.func(...); The function proxy can then be called from Node.js like any asynchronous function: myFunction('Some input', function (error, result) {
//...
}); Alternatively, if you know the C# implementation will complete synchronously given the circumstances, you can call this function as any synchronous JavaScript function as follows: var result = myFunction('Some input', true); The One representation of CLR code that Edge.js accepts is C# source code. You can embed C# literal representing a .NET async lambda expression implementing the var add7 = edge.func('async (input) => { return (int)input + 7; }'); In another representation, you can embed multi-line C# source code by providing a function with a body containing a multi-line comment. Edge extracts the C# code from the function body using regular expressions: var add7 = edge.func(function() {/*
async (input) => {
return (int)input + 7;
}
*/}); Or if you use ES6 you can use template strings to define a multiline string: var add7 = edge.func(`
async (input) => {
return (int)input + 7;
}
`); If your C# code is more involved than a simple lambda, you can specify entire class definition. By convention, the class must be named var add7 = edge.func(function() {/*
using System.Threading.Tasks;
public class Startup
{
public async Task<object> Invoke(object input)
{
int v = (int)input;
return Helper.AddSeven(v);
}
}
static class Helper
{
public static int AddSeven(int v)
{
return v + 7;
}
}
*/}); If your C# code grows substantially, it is useful to keep it in a separate file. You can save it to a file with var add7 = edge.func(require('path').join(__dirname, 'add7.csx')); If you integrate C# code into your Node.js application by specifying C# source using one of the methods above, edge will compile the code on the fly. If you prefer to pre-compile your C# sources to a CLR assembly, or if your C# component is already pre-compiled, you can reference a CLR assembly from your Node.js code. In the most generic form, you can specify the assembly file name, the type name, and the method name when creating a Node.js proxy to a .NET method: var clrMethod = edge.func({
assemblyFile: 'My.Edge.Samples.dll',
typeName: 'Samples.FooBar.MyType',
methodName: 'MyMethod' // This must be Func<object,Task<object>>
}); If you don't specify methodName, The assemblyFile is relative to the working directory. If you want to locate your assembly in a fixed location relative to your Node.js application, it is useful to construct the assemblyFile using You can also create Node.js proxies to .NET functions specifying just the assembly name as a parameter: var clrMethod = edge.func('My.Edge.Samples.dll'); In that case the default typeName of How to: specify additional CLR assembly references in C# codeWhen you provide C# source code and let edge compile it for you at runtime, edge will by default reference only mscorlib.dll and System.dll assemblies. If you're using .NET Core, we automatically reference the most recent versions of the System.Runtime, System.Threading.Tasks, System.Dynamic.Runtime, and the compiler language packages, like Microsoft.CSharp. In applications that require additional assemblies you can specify them in C# code using a special hash pattern, similar to Roslyn. For example, to use ADO.NET you must reference System.Data.dll: var add7 = edge.func(function() {/*
#r "System.Data.dll"
using System.Data;
using System.Threading.Tasks;
public class Startup
{
public async Task<object> Invoke(object input)
{
// ...
}
}
*/}); If you prefer, instead of using comments you can specify references by providing options to the var add7 = edge.func({
source: function() {/*
using System.Data;
using System.Threading.Tasks;
public class Startup
{
public async Task<object> Invoke(object input)
{
// ...
}
}
*/},
references: [ 'System.Data.dll' ]
}); If you are using .NET Core and are using the .NET Core SDK and CLI, you must have a
Edge.js also supports running published .NET Core applications on servers that do not have the .NET Core SDK and CLI installed, which is a common scenario in production environments. To do so, the
On your development machine, you would run How to: marshal data between C# and Node.jsEdge.js can marshal any JSON-serializable value between .NET and Node.js (although JSON serialization is not used in the process). Edge also supports marshalling between Node.js You can call .NET from Node.js and pass in a complex JavaScript object as follows: var dotNetFunction = edge.func('Edge.Sample.dll');
var payload = {
anInteger: 1,
aNumber: 3.1415,
aString: 'foo',
aBoolean: true,
aBuffer: new Buffer(10),
anArray: [ 1, 'foo' ],
anObject: { a: 'foo', b: 12 }
};
dotNetFunction(payload, function (error, result) { }); In .NET, JavaScript objects are represented as dynamics (which can be cast to using System.Threading.Tasks;
public class Startup
{
public async Task<object> Invoke(dynamic input)
{
int anInteger = (int)input.anInteger;
double aNumber = (double)input.aNumber;
string aString = (string)input.aString;
bool aBoolean = (bool)input.aBoolean;
byte[] aBuffer = (byte[])input.aBuffer;
object[] anArray = (object[])input.anArray;
dynamic anObject = (dynamic)input.anObject;
return null;
}
}
Similar type marshalling is applied when .NET code passes data back to Node.js code. In .NET code you can provide an instance of any CLR type that would normally be JSON serializable, including domain specific types like using System.Threading.Tasks;
public class Person
{
public int anInteger = 1;
public double aNumber = 3.1415;
public string aString = "foo";
public bool aBoolean = true;
public byte[] aBuffer = new byte[10];
public object[] anArray = new object[] { 1, "foo" };
public object anObject = new { a = "foo", b = 12 };
}
public class Startup
{
public async Task<object> Invoke(dynamic input)
{
Person person = new Person();
return person;
}
} In your Node.js code that invokes this .NET method you can display the result object that the callback method receives: var edge = require('edge');
var getPerson = edge.func(function () {/*
using System.Threading.Tasks;
public class Person
{
public int anInteger = 1;
public double aNumber = 3.1415;
public string aString = "foo";
public bool aBoolean = true;
public byte[] aBuffer = new byte[10];
public object[] anArray = new object[] { 1, "foo" };
public object anObject = new { a = "foo", b = 12 };
}
public class Startup
{
public async Task<object> Invoke(dynamic input)
{
Person person = new Person();
return person;
}
}
*/});
getPerson(null, function (error, result)
全部评论
专题导读
上一篇:amplitude/Amplitude-iOS: Native iOS/tvOS/macOS SDK发布时间:2022-08-18下一篇:MaikuB/flutter_local_notifications: A Flutter plugin for displaying local notifi ...发布时间:2022-08-18热门推荐
热门话题
阅读排行榜
|
请发表评论