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

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

所以我们有一个小型的node.js Web 服务器,它使用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中的一个方法,该方法将返回一个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# 函数没有正确返回数据?这也可能是异步问题吗?

最佳答案

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

关于c# - 在 Node.js 中调用 C# 代码时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204983/

相关文章:

c# - 在同一事务中删除和添加子实体混淆了 nhibernate

c# - .NET : Monitoring Objects Lifetime (Birth/Death/Memory)

mysql - 包含文件时 node.js 上的引用错误

swift - 如何将异步函数的结果返回到 Swift 中的不同类

javascript - 使用具有自定义延迟的 javascript 在循环中动态更改包装器背景

ios - 如何扩展使用ASIHTTP 异步加载图像的UIImageView 的功能?

c# - SmtpClient 非常慢 - 发送一封非常小的电子邮件大约需要 2 秒

c# - SingleOrDefault 和 FirstOrDefault 返回缓存数据

javascript - 如何使用客户端从套接字发出的 json 数据?

javascript - 使用 Puppeteer 在网站中查找最大的图像