使用 .NET Core 2.2 网络连接到 Neo4j Aura api

Connecting to Neo4j Aura with .NET Core 2.2 web api

我正在尝试从 .NET core 2.2 web api 连接到 Neo4j Aura 实例。我知道我需要 Neo4j .Net Driver v4.0.0-alpha01,但我似乎无法连接。没有很多例子,因为这个驱动程序是新的,Aura 也是。

我不断收到: Failed after retried for 6 times in 30000 ms. Make sure that your database is online and retry again.

我这样配置驱动程序

public void ConfigureServices(IServiceCollection services)
{
   string uri = "neo4j://1234567.databases.neo4j.io:7687";//not actual subdomain
   string username = "neo4j";
   string password = "seeeeeeecret";//not actual password

   services.AddCors();
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
   services.AddSingleton(GraphDatabase.Driver(uri, AuthTokens.Basic(username, password)));
}

在我的测试控制器中 运行 这个

private async Task<string> Neo4JTestAsync()
        {
            string db = "MyDb";
            string message = "TESTMESSAGE";

            IAsyncSession session = _driver.AsyncSession(o => o.WithDatabase(db));
            try
            {
                var greeting = session.WriteTransactionAsync(async tx =>
                {
                    var result = tx.RunAsync("CREATE (a:Greeting) " +
                                        "SET a.message = $message " +
                                        "RETURN a.message + ', from node ' + id(a)",
                        new { message });
                    var res = await result;
                    return "return something eventually";
                });
                return await greeting;
            }
            catch (Exception e)
            {
                return e.Message; // throws "Failed after retried for 6 times in 30000 ms. Make sure that your database is online and retry again"
            }
            finally
            {
                await session.CloseAsync();
            }
        }

您尝试过在连接字符串中使用 bolt:// 吗?

string uri = "bolt://1234567.databases.neo4j.io:7687";//not actual subdomain

我无法得到你的 exact 错误信息 - 但我很确定这是由于加密 - 1.x4.x 驱动程序是加密的默认位置 - 现在默认 关闭

因此您需要将初始化更改为:

 services.AddSingleton(GraphDatabase.Driver(uri, AuthTokens.Basic(username, password), config => config.WithEncryptionLevel(EncryptionLevel.Encrypted)));

这应该让你继续。另外 - 确保你坚持使用 neo4j:// 协议,因为它会正确地路由你。