Mono is no longer actively supported. Exisitng code will remain with no plans of removing it but the focus will be on .NET Framework and .NET Core.
Mono tests are now excluded from CI on Travis and AppVeyor.
Edge.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:
script Node.js from a .NET application (console app, ASP.NET, etc.)
script C# from a Node.js application on Windows, MacOS, and Linux
If 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 need
Edge.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.
Edge.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:
> docker run -it tjanczuk/edgejs:6.5.1
> cd samples
> node 101_hello_lambda.js
.NET welcomes Node.js
Specify the EDGE_USE_CORECLR=1 environment variable to use .NET Core instead:
> docker run -it tjanczuk/edgejs:6.5.1
> cd samples
> EDGE_USE_CORECLR=1 node 101_hello_lambda.js
.NET welcomes Node.js
Alternatively, you can also specify EDGE_USE_CORECLR when starting the container:
> docker run -it -e EDGE_USE_CORECLR=1 tjanczuk/edgejs:6.5.1
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 EDGE_USE_CORECLR environment variable:
Edge 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 Func<object,Task<object>> delegate. This allows Node.js code to call .NET asynchronously and avoid blocking the Node.js event loop.
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 Func<object,Task<object>> .NET delegate:
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:
varresult=myFunction('Some input',true);
The true parameter instead of a callback indicates that Node.js expects the C# implementation to complete synchronously. If the CLR function implementation does not complete synchronously, the call above will result in an exception.
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 Func<object,Task<object>> delegate directly inside Node.js code:
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:
If your C# code is more involved than a simple lambda, you can specify entire class definition. By convention, the class must be named Startup and it must have an Invoke method that matches the Func<object,Task<object>> delegate signature. This method is useful if you need to factor your code into multiple methods:
varadd7=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 *.csx or *.cs extension, and then reference from your Node.js application:
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:
varclrMethod=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, Invoke is assumed. If you don't specify typeName, a type name is constructed by assuming the class called Startup in the namespace equal to the assembly file name (without the .dll). In the example above, if typeName was not specified, it would default to My.Edge.Samples.Startup.
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 __dirname. If you are using .NET Core, assemblyFile can also be a project name or NuGet package name that is specified in your project.json or .deps.json dependency manifest.
You can also create Node.js proxies to .NET functions specifying just the assembly name as a parameter:
varclrMethod=edge.func('My.Edge.Samples.dll');
In that case the default typeName of My.Edge.Samples.Startup and methodName of Invoke is assumed as explained above.
How to: specify additional CLR assembly references in C# code
When 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:
varadd7=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 edge.func call:
varadd7=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 project.json file (specification here) that specifies the dependencies for the application. This list of dependencies must also include the Edge.js runtime package and, if you need to be able to dynamically compile your code, the package(s) for the compilers that you plan to use, like Edge.js.CSharp. You must have run the dotnet restore (to restore the dependencies) and dotnet build (to build your project and generate the dependency manifest) commands in that project's directory to generate a .deps.json file under bin/[configuration]/[framework], i.e. bin/Release/netstandard1.6/MyProject.deps.json. This .deps.json file must either be in the current working directory that node is executed in or you must specify its directory by setting the EDGE_APP_ROOT environment variable. For example, if for a netstandard1.6 project in the c:\DotNet\MyProject directory, you would run something like:
set EDGE_APP_ROOT=c:\DotNet\MyProject\bin\Release\netstandard1.6
node app.js
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 .csproj for your application should meet the following requirements:
It should target the netcoreapp1.x, netcoreapp2.x, netstandard1.6 or netstandard2.0 framework moniker.
It should reference Microsoft.NETCore.DotNetHost and Microsoft.NETCore.DotNetHostPolicy. This is required so that the publish process can provide all the native libraries required to create a completely standalone version of your application.
<PreserveCompilationContext>true</PreserveCompilationContext> and <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> should be present under <PropertyGroup>. You can add an empty Main() implementation to your project to accommodate it; this method will not be called, but is just a requirement in order for dotnet publish to generate a completely standalone app.
On your development machine, you would run dotnet publish -r [target runtime for your production server] (i.e. dotnet publish -r ubuntu.14.04-x64) to aggregate the package assemblies and native libraries necessary to run your application. You can copy the contents of the publish directory up to your SDK- and CLI-less server and use them directly in Edge.js by setting the EDGE_APP_ROOT environment variable to the directory on the server that you copied the published application to.
How to: marshal data between C# and Node.js
Edge.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 Buffer instance and a CLR byte[] array to help you efficiently pass binary data.
You can call .NET from Node.js and pass in a complex JavaScript object as follows:
In .NET, JavaScript objects are represented as dynamics (which can be cast to IDictionary<string,object> if desired), JavaScript arrays as object[], and JavaScript Buffer as byte[]. Scalar JavaScript values have their corresponding .NET types (int, double, bool, string). Here is how you can access the data in .NET:
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 Person or anonymous objects. For example:
请发表评论