-
Notifications
You must be signed in to change notification settings - Fork 147
connecting
You first need to include a reference to the Neo4jClient
nuget package. This will add all the required dependencies to the project.
There are two classes which connect to Neo4j, the choice between them comes down to how you want to connect to your server (or indeed how you can).
-
GraphClient
- provides anHTTP
based connection -
BoltGraphClient
- provides aBolt
based connection
This is the entry point for all further operations. For testability they both implement the IGraphClient
interface. This also means you can swap between the two clients without changing the rest of your codebase.
You need to instantiate, then connect the client prior to use:
//HTTP
var graphClient = new GraphClient("http://localhost:7474/db/data", "username", "password");
await graphClient.ConnectAsync();
//Bolt
var boltGraphClient = new BoltGraphClient("neo4j://localhost:7687", "username", "password");
await boltGraphClient.ConnectAsync();
The ConnectAsync
method sends a request to the root of the REST/Bolt API to discover where the required
API endpoints are. As such, the ConnectAsync
method needs to be called before anything else is
called on the client.
(Development note: ConnectAsync
was implemented as a separate method because
constructors shouldn't throw exceptions.)
GraphClient
and BoltGraphClient
are thread safe. You should only have one instance of either for each database that you
want to talk to (typically, one). This avoids excess calls to the ConnectAsync()
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("http://localhost:7474/db/data", "username", "password");
var connectTask = graphClient.ConnectAsync();
connectTask.Wait();
return graphClient;
})
.SingleInstance();
The BoltGraphClient
should be used in the same way:
containerBuilder
.Register<IGraphClient>(context =>
{
var graphClient = new BoltGraphClient("neo4j://localhost:7687", "username", "password");
var connectTask = graphClient.ConnectAsync();
connectTask.Wait();
return graphClient;
})
.SingleInstance();
This only applies to the GraphClient
class.
You can use a single NeoServerConfiguration
object to create a GraphClient
per request instead of using a Singleton.
This may be beneficial for hi-transaction environments where a ConcurrentTransactionException
may occur on the server when using a single GraphClient
instance.
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();
}
}
}