调用Node.js中的C#代码时出现意外的结果

所以我们有一个小型的node.jsnetworking服务器,它使用edge.js连接到一个C#写的DLL。 在我们的C#代码(这只是一个类库),我们正在调用一个方法,执行一个查询到Neo4j。 现在,如果我们在控制台应用程序中testing它,它可以正常工作,但是当我们运行node.js时,我们得到以下exception:

System.AggregateException: One or more errors occurred. ---> System.TypeInitiali zationException: The type initializer for 'FriendLibrary.API.Searching' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object. at FriendLibrary.API.Searching..cctor() --- End of inner exception stack trace --- at FriendLibrary.API.Searching..ctor() at Startup.<<Invoke>b__1>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at Startup.<<Invoke>b__0>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at Startup.<Invoke>d__9.MoveNext() --- End of inner exception stack trace --- ---> (Inner Exception #0) System.TypeInitializationException: The type initializ er for 'FriendLibrary.API.Searching' threw an ex ception. ---> System.NullReferenceException: Object reference not set to an inst ance of an object. at FriendLibrary.API.Searching..cctor() --- End of inner exception stack trace --- at FriendLibrary.API.Searching..ctor() at Startup.<<Invoke>b__1>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at Startup.<<Invoke>b__0>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at Startup.<Invoke>d__9.MoveNext()<--- 

我们试图做的是调用dll中的方法,将返回一个json数组,node.js将传递到前端,但是我们不能返回json数组?

我们的C#代码如下:

  public string GetAllFriends() { string jsonArray = null; Query query = new Query(); //This is a basic cypher query that returns an IEnumerable<Person> var result = query.GetAllFriends(); List<Person> list = new List<Person>(); foreach (var node in result ) { list.Add(new Person { Age= node.Data.Age, Name = node.Data.Name, }); } //Our result must be returned as a json string jsonObject = JsonConvert.SerializeObject(list); return jsonObject; } 

上面的代码在我们的testing控制台应用程序中100%工作。

现在为node.js代码:

 var express = require("express"); var app = express(); var hitThinDLL = require('edge').func(function(){/* #r "FriendLibrary.dll" using FriendLibrary.API.Searching; async(input)=>{ return await Task.Run<object>(async () => { Console.WriteLine("Entering C# Async Function, sleeping for 5sec"); ContentRetrieval ct = new ContentRetrieval(); string json = ct.GetAllFriends(); Console.WriteLine("Sleep done, returning changes."); return json; }); } */}); app.get("/", function(req, res) { hitThinDLL(null, function(error, result){ if (error) throw error; console.log(result); }); res.end(); }); setInterval(function() { console.log('Node.js event loop is alive'); }, 1000); app.listen(8989); 

难道是C#函数没有正确返回数据? 这也可能是一个asynchronous的问题?

这可能听起来很愚蠢,但是前段时间我也遇到了这个问题。 当用不同版本的DLLtesting这个时,我们发现这个。 我们更深层的.dll所需的dll不在节点文件夹中。