Neo4J Client connection error

902 views Asked by At

I'm new to neo4j and having trouble t connect to the Neo4J server wth .NET.

I have installed both the Neo4j DB (v2.2.2) and neo4jclient (v 1.0.0.662) with Install-Package Neo4jclient in Visual Studio, created an empty project with the below code

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

The Problem is that I get an error saying that I'm missing GraphClient.cs. Is there anything else I need to install or download?

Thanks

1

There are 1 answers

0
Charlotte Skardon On

EDIT: Since this was originally written the client has moved on a bit (any version > 1.1.0.0), and you can pass in the auth details on construction (which is the preferred way):

var client = new GraphClient(new Uri("URI"), "User", "Pass"));

Because you're on 2.2x of Neo4j, you'll have a user/pass element (unless you've turned it off), so you have 2 options, 1 turn off the authentication, 2 send the authentication through with your calls.

Method 1 - Disable Authentication

Personally - for local dev work I tend to turn it off. To do this, go to the place you installed Neo4j, and go to the conf folder. In there, edit the neo4j-server.properties file. Roughly around line 24 there should be a line reading:

dbms.security.auth_enabled=true

All you need to do is set that to false and you should find your connection will work.

Method 2 - Send Authentication Details

You'd need to do this for a live version (unless you plan to not use auth with your db), you have two options both detailed here: Upgrading your Neo4j to 2.2 and having Auth trouble? The quick way is to pass the auth stuff with the URI, so instead of http://localhost.:7474/db/data you do something like: http://username:password@localhost.:7474/db/data.

The other route involves injecting the Authentication Header into all the requests made - you create a client authentication wrapper:

private class HttpClientAuthWrapper : IHttpClient
{
    private readonly AuthenticationHeaderValue _authenticationHeader;
    private readonly HttpClient _client;

    public HttpClientAuthWrapper(string username, string password)
    {
        _client = new HttpClient();
        if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            return;

        var encoded = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));
        _authenticationHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoded));
    }

    public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
    {
        if(_authenticationHeader != null)
            request.Headers.Authorization = _authenticationHeader;
        return _client.SendAsync(request);
    }
}

and then inject it into Neo4jClient like so:

var client = new GraphClient(
    new Uri("http://localhost.:7474/db/data"), 
    new HttpClientAuthWrapper("user", "pass#")
    );
client.Connect();

the choice, as they say, is yours.