2016-12-13 111 views
0

我想从第三方dll使用Tomasz Janczuk的awesome edgejs库调用一个函数。根据我收到的错误消息,代码可能会或可能不会返回信息。错误信息(见下文)我得到说代码需要一个字符串,但是取而代之的是取回ExpandoObject。我已经尝试了几个不同版本的代码,这些代码基于edgejs github页面上的示例,但它们都返回相同的错误。这个错误表明它只是将一个字符串转换为一个扩展对象,但到目前为止我没有成功。任何有关这个问题的帮助将非常感谢。我一直在努力争取这个工作的时间比我想要承认的要长。基于110_clr_instance.js样品在edgejs github上Edge.js强制转换为字符串expandoobject

代码:

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,并从边缘发现了这个错误。 JS。 ReferenceError: options is not defined

edgejs_clr_instance_error

回答

1

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

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

不是

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

感谢很多的帮助托马斯! – SnowballsChance

相关问题