所以我们有一个使用 edge.js 连接到 C# 编写的 dll 的小型 node.js 网络服务器。在我们的 c# 代码(只是一个类库)中,我们调用了一个对 Neo4j 执行查询的方法。现在,如果我们在控制台应用程序中测试它,它可以正常工作,但是当我们运行 node.js 时,我们会得到以下异常:
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;
}
上面的代码在我们的测试控制台应用程序中 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# 函数没有正确返回数据?它也可能是一个异步问题吗?