繁体   English   中英

Edge.js从ExpandoObject转换为字符串

[英]Edge.js cast to string from expandoobject

我正在尝试使用Tomasz Janczuk的很棒的edgejs库从第三方dll调用函数。 根据我收到的错误消息,代码可能会或可能不会返回信息。 我收到的错误消息(请参阅下文)说代码在期待一个字符串,但是却取回了ExpandoObject。 我已经基于edgejs github页面上的示例尝试了该代码的几种不同版本,但是它们都返回相同的错误。 该错误表明这只是将expandoobject强制转换为字符串的问题,但到目前为止,我还没有成功。 任何有关此问题的帮助将不胜感激。 我一直在努力使它工作的时间超过我所承认的时间。

Edgejs github上基于110_clr_instance.js示例的代码:

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));

控制台错误输出:

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

但是后来我使用webstorm逐步检查了代码,并从edge.js发现了此错误。 ReferenceError: options is not defined

edgejs_clr_instance_error

您将C#文字传递给edge.func的方式缺少function () {...}包装器。 它应该是:

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

而不是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM