Search code examples
c#roslyncoreclredgejs

How to reference Roslyn assemblies from an EdgeJS app?


I'd like to use a Roslyn library from an EdgeJS application but am not sure how to reference it properly. This is the code in question:

var edge = require('edge');
var toroslyn = edge.func(function(){/*
   using System;
   using System.Threading.Tasks;
   using Microsoft.CodeAnalysis.CSharp.Scripting;
   public class Startup
   {
      public async Task<object> Invoke(object code)
      {
         return await CSharpScript.EvaluateAsync("1 + 2");
      }
   }
*/
});
toroslyn(null, function (error, result) { console.log(result); });

And the project.json:

{
  "dependencies": {
     "Microsoft.CodeAnalysis.CSharp.Scripting": "1.1.1",
  },
  "frameworks": {
     "dnxcore50": {}
  }
}

It would be nice if after doing dnu restore it just worked, as is, but the documentation states I need to reference the dependencies inline like #r "Microsoft.CodeAnalysis.CSharp.Scripting.dll".

However, for it to work I need to specify the whole path to the assembly like #r "C:/Users/Christian/.dnx/packages/....../Microsoft.CodeAnalysis.CSharp.Scripting.dll" alternatively make sure all dependent assemblies are located in the same folder as my node app.

Are these the two options I have or am I missing something?


Solution

  • You can also specify fully qualified reference assembly names as an array passed in to the edge.func call as described at https://github.com/tjanczuk/edge#how-to-specify-additional-clr-assembly-references-in-c-code:

    var add7 = edge.func({
        source: function() {/*
    
            using System.Data;
            using System.Threading.Tasks;
    
            public class Startup
            {
                public async Task Invoke(object input)
                {
                    // ...
                }
            }
        */},
        references: [ 'System.Data.dll' ]
    );
    

    You may find it easier to constuct fully qualified assembly names in JavaScript rather than embed as part of the #r directive in code.