DotNet4Neo4j / Neo4jClient

.NET client binding for Neo4j
https://www.nuget.org/packages/Neo4jClient
Microsoft Public License
431 stars 146 forks source link

Working with Aura DB #445

Open mcalvin-kinectify opened 2 years ago

mcalvin-kinectify commented 2 years ago

Hello, We have been using this client on a local neo4j instance and have upgraded to Neo4j's hosted Aura database instead. Aura appears to only support the neo4j+s bolt protocol, and I am getting a messages that the protocol is not supported by Neo4jClient. Is that correct and the client won't work with Aura or am I doing something wrong? Here is how I am injecting the client into the dependency injection container:

 services.AddSingleton(context =>
  {
      var path = config.GetValue<string>("Neo4j:Path");
      if (!path.StartsWith("http") && !path.StartsWith("neo4j")) // note with aura db this condition would not be met (starts with neo4j+s)
      {
          path = $"http://{path}:7474"; // add the neo4j protocol and port if it isn't provided
      }

      return NeoServerConfiguration.GetConfigurationAsync(
          new Uri(path),
          config.GetValue<string>("Neo4j:User"),
          config.GetValue<string>("Neo4j:Password")
      ).GetAwaiter().GetResult();
  });

  services.AddSingleton<IGraphClientFactory, GraphClientFactory>();
mcalvin-kinectify commented 2 years ago

Here is the exception I am receiving: image

mcalvin-kinectify commented 2 years ago

I was able to get a successful connection by doing this:

services.AddScoped(context =>
{
    var path = config.GetValue<string>("Neo4j:Path");

    return GraphDatabase.Driver(path, AuthTokens.Basic(config.GetValue<string>("Neo4j:User"),
        config.GetValue<string>("Neo4j:Password")));
});

services.AddScoped<IBoltGraphClient>(x =>
{
    var driver = x.GetService<IDriver>();
    return new BoltGraphClient(driver);
});

and then injecting IBoltGraphClient instead of IGraphClientFactory. Unfortunately, if the BoltGraphClient gets disposed, the underlying driver is disposed also. So it doesn't look like I can reuse the GraphDatabase.Driver and have to register it as Scoped instead of a Singleton.

cjpomer commented 1 year ago

I have run into the same issue working with Aura DB.

@mcalvin-kinectify for my understanding/curiosity, the issue with a Scoped lifetime is that you have to call BoltGraphClient.ConnectAsync with each connection, right?