繁体   English   中英

如何处理新的 ConnectAsync API (C# Neo4jClient)

[英]How to handle the new ConnectAsync API (C# Neo4jClient)

我将 Neo4JClient 更新到 4.x 版本,其中包括从client.Connect(); client.ConnectAsync() 当向 api 发出请求时,连接到 neo4j 的 controller 正在实例化,因此在连接和使用连接之间没有太多时间。 异步连接所以我的猜测让我遇到了问题,当我已经发出数据库请求时客户端没有连接。 我试图通过等待未解决问题的ResultsAsync来解决此问题。 目前没有文档存在,因为它已经过时了。 这是我的代码:

        // The client is getting injected from my server
        public MyService(GraphClient client)
        {
            this.client = client;
            this.client.ConnectAsync();
        }

        public async Task<bool> Login(string username, string password)
        {
            var query = client.Cypher
                .Match("(n)")
                .Where("n.username = '" + username + "' AND n.password = '" + password + "'")
                .Return(n => n.As<User>());

            var queryResult = await query.ResultsAsync;
            return queryResult.ToList().Count == 1;
        }

在您的构造函数中,您将执行以下操作:

var task = this.client.ConnectAsync();
task.Wait();

或者你可以这样做:

this.client.ConnectAsync().Wait();

还有一些额外的事情,你注入的GraphClient应该是一个IGraphClient - 如果你以后决定更改为 Bolt 版本,这将使你更容易。

另一个更重要的事情是,注入它的服务器应该已经完成了ConnectAsync位 - 你只想调用一次,如果它被注入到多个类中,它将被多次调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM