connecting

Miroslav Jelaska edited this page Mar 11, 2017 · 7 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([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();

New from 1.1.0.29+ You can use single neo server configuration object and use graph client per request instead of singleton. This may be beneficial for hi-transaction environments where concurrent transaction exception may occur on the server when using single graph client instance.

Use case

Composition root (autofac example):

    public void Register()
    {
        containerBuilder
            .Register<NeoServerConfiguration>(context => NeoServerConfiguration.GetConfiguration(uri, user, pwd))
            .SingleInstance();

        containerBuilder
            .RegisterType<GraphClientFactory>()
            .As<IGraphClientFactory>()
            .SingleInstance();
    }

Service level:

    public class NeoServiceClass
    {
        private readonly IGraphClientFactory factory;
        ...
        public async Task ExecuteQueryAsync()
        {
            using (var client = factory.Create())
            {
                // query body
                await client.Cypher.Query.ExecuteWithoutResultsAsync();
            }
        }
    }

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.

New from 1.1.0.0+ we support passing in the user/pass via the constructor:

    var client = new GraphClient(new Uri("http://localhost:7474/db/data"), "user", "pass");
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.