Warm tip: This article is reproduced from serverfault.com, please click

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

(neo4j - How to handle the new ConnectAsync API (C# Neo4jClient))

发布于 2020-12-04 05:54:59

我将Neo4JClient更新为版本4.x,其中包括从client.Connect();到的重构client.ConnectAsync()发出对api的请求时,将实例化连接到neo4j的控制器,因此在连接和使用连接之间没有太多时间。异步连接使我的猜测困扰我,当我已经在发出数据库请求时,客户端没有连接。我想通过等待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;
        }
Questioner
TobiasW
Viewed
0
Charlotte Skardon 2020-12-04 20:11:41

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

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

或者,你可以执行以下操作:

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

还有两件事,GraphClient你注入的内容确实应该是IGraphClient-如果你以后决定更改为Bolt版本,这将使你更轻松。

另一个更重要的事情是,注入它的服务器应该已经完成​​了这一工作ConnectAsync-你只想调用一次,并且如果将它注入多个类中,则将多次调用它。