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

时间:2013-08-13 09:09:24

标签: c# node.js asynchronous neo4jclient edge.js

所以我们有一个小的node.js webserver,它使用edge.js连接到C#编写的dll。 在我们的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中调用一个方法,该方法将返回一个node.js将传递给前端的json数组,但是我们不能返回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#函数没有正确返回数据吗?它也可能是一个异步问题吗?

1 个答案:

答案 0 :(得分:2)

这可能听起来很愚蠢,但我前段时间也遇到过这个问题。当我们使用不同版本的DLL对此进行测试时,我们发现我们更深层.dll所需的。 dll不在节点文件夹中。