Search code examples
javascriptc#node.jsexpandoobjectedgejs

Edge.js cast to string from expandoobject


I'm trying to call a function from a 3rd party dll using Tomasz Janczuk's awesome edgejs library. According to the error messages I'm getting, the code may or may not be returning information. The error message (see below) I'm getting says the code is expecting a string but is getting an ExpandoObject back instead. I've tried several different versions of this code based on the samples on the edgejs github page but all of them return the same error. The error would suggest it's just a matter of casting the expandoobject to a string but so far I'm having no success. Any help with this problem would be MUCH appreciated. I've been struggling to get this to work for longer than I care to admit.

Code based on 110_clr_instance.js sample at edgejs github:

var edge = require('edge');

var createSerial = edge.func({
    source: {/*
    using System;
    using System.Threading.Tasks;

    public class Startup
    {
        public async Task<object> Invoke(string location)
        {
            var serial = new Serial(location);
            return new {
                validateLocation = (Func<object,Task<object>>)(
                    async (location) =>
                    { 
                        serial.ValidateLocation((string)location);
                        return serial.IsValid;
                    }
                )
            };
        }
    }

    public class Serial
    {
        public static Connection conn = new Connection("https://companyname.moraware.net/companyname/api.aspx", "username", "password");
        public bool IsValid { get; private set; }

        public Serial(bool isValid)
        {
            this.IsValid = isValid;
        }

        public void ValidateLocation(string location)
        {
         conn.AutoRefreshOnTimeout = true;
         if (!conn.Connected) { conn.Connect(); }
            this.IsValid = conn.GetInventoryLocations().Any(x => x.InventoryLocationName == location);
         if (conn.Connected) { conn.Disconnect(); }
        }
    }    */},
    references: ['JobTrackerAPI4.dll','Newtonsoft.Json.dll']

    });

var serial = createSerial("A01-01", true);
console.log(serial.validateLocation(null, true));

Console Error Output:

C:\Users\csnow\NodeApps\MarshallDataEdge\node_modules\edge\lib\edge.js:161
return edge.initializeClrFunc(options);
            ^
Error: Unable to cast object of type 'System.Dynamic.ExpandoObject' to type 'System.String'.
    at Error (native)
    at Object.exports.func (C:\Users\csnow\NodeApps\MarshallDataEdge\node_modules\edge\lib\edge.js:161:17)
    at Object.<anonymous> (C:\Users\csnow\NodeApps\MarshallDataEdge\Evolveware_clr_instance.js:5:25)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

Process finished with exit code 1

But then I stepped through code using webstorm and discovered this error from edge.js. ReferenceError: options is not defined

edgejs_clr_instance_error


Solution

  • The way you pass in the C# literal to edge.func is missing the function () {...} wrapper. It should be:

    edge.func({
      source: function () {/*
        // C# code goes here
      */},
      ...
    });
    

    Rather than

    edge.func({
      source: {/*
        // C# code goes here
      */},
      ...
    });