Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memgraph C# Connection Issue (Session NullReferenceException) using Neo4j.Driver

I need to connect to my memgraph server (runs through docker), but any basic example I try ends up with a null reference exception when running any query.

The code I am using is:

public async void DatabaseConnectionTest()
    {
        IDriver driver = GraphDatabase.Driver("bolt://127.0.0.1:7687", AuthTokens.None);
        IAsyncSession session = driver.AsyncSession();
        try
        {
            IResultCursor cursor = await session.RunAsync("CREATE (n: TestNode) RETURN n");
            await cursor.ConsumeAsync();
        }
        catch(Exception e)
        {
            Console.WriteLine($"Exception {e.Message}");
        }
        finally
        {
            await session.CloseAsync();
        }

        await driver.DisposeAsync();
    }

When trying to run this code, I get a "NullReferenceException" to this line:

IResultCursor cursor = await session.RunAsync("CREATE (n: TestNode) RETURN n");

Am I doing something completely wrong? What am I missing.

Thanks in advance!

like image 758
zackper Avatar asked Oct 29 '25 19:10

zackper


1 Answers

The problem I was facing was due to wrong configuration file for my memgraph docker container.

In the documentation of Memgraph (as seen here) it explicitly declares that to use the Neo4J.Driver in you need to apply the following changes.

In order for the Neo4j driver to work, you need modify configuration setting --bolt-server-name-for-init. When running Memgraph, set --bolt-server-name-for-init=Neo4j/5.2.0. If you use other version of Neo4j driver, make sure to put the appropriate version number.

Now it works perfectly!

like image 94
zackper Avatar answered Oct 31 '25 08:10

zackper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!