Pregunta

So we have a small node.js webserver that uses edge.js to connect to a C# written dll. In our c# code(which is just a class library) we are calling a method that performs a query to Neo4j. Now If we test this in a console app, it works fine, however when we run node.js we get the following 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()<---

What we are trying to do is call a method in the dll that will return a json array that node.js will pass to the front end, however we cannot return the json array?

Our c# code as follows:

    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;
    }

The code above works 100% in our test Console Application.

Now for the node.js code:

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);

Could it be that the C# function is not returning the data correctly? Could it also be an async problem?

¿Fue útil?

Solución

This might sound stupid but I had this problem some time ago as well. When testing this with different versions of our DLL it occurred to us that the .dlls required by our deeper tier .dlls weren't in the node folder.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top