Skip to content
Erick Calder edited this page Jun 3, 2015 · 10 revisions

Getting Connected

To get connected we first include a reference to the library in our Visual Studio project. In Powershell, we can load the library (and its dependencies) like this:

C:> [System.Reflection.Assembly]::LoadFrom("c:\...\NewtonSoft.Json.dll")
C:> [System.Reflection.Assembly]::LoadFrom("c:\...\Neo4jClient.dll")

Graph Client Basics

They key class you need to know about is GraphClient. This is the entry point for all further operations. For testability purposes, it implements IGraphClient.

You need to instantiate, then connect the client prior to use:

var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

In Powershell that would be:

C:> $neo = new-object Neo4jClient.GraphClient(new-object Uri("http://localhost:7474/db/data"));
C:> $neo.Connect()

The Connect method sends a request to the root of the REST API to discover where the required API endpoints are. As such, the Connect method needs to be called before anything else is called on the client.

(Development note: Connect was implemented as a separate method because constructors shouldn't throw exceptions.)

Threading and Lifestyles

GraphClient is thread safe. You should only have one instance of it for each database that you want to talk to (typically, one). This avoids excess calls to the Connect() method which requires a roundtrip to the neo4j server.

As an example, if you were using Autofac, you could register the graph client like so:

containerBuilder
    .Register<IGraphClient>(context =>
    {
        var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
        graphClient.Connect();
        return graphClient;
    })
    .SingleInstance();

Passing Credentials

We support the standard URI syntax for basic authentication credentials:

var client = new GraphClient(new Uri("http://user:pass@localhost:7474/db/data"));

This is useful for connecting to hosted instances on places like Heroku.

Clone this wiki locally