CosmosDB C#Gremlin-发送查询时获取异常

时间:2018-10-10 20:33:58

标签: azure-cosmosdb gremlin

来自:https://docs.microsoft.com/en-us/azure/cosmos-db/create-graph-dotnet

在.wait()部分出现异常:

   NullReferenceException: Object reference not set to an instance of an object.

   at Gremlin.Net.Driver.Connection.ReceiveAsync[T]()
   at Gremlin.Net.Driver.Connection.SubmitAsync[T](RequestMessage requestMessage)
   at Gremlin.Net.Driver.ProxyConnection.SubmitAsync[T](RequestMessage requestMessage)
   at Gremlin.Net.Driver.GremlinClient.SubmitAsync[T](RequestMessage requestMessage)
   at Gremlin.Net.Driver.GremlinClientExtensions.SubmitAsync[T](IGremlinClient gremlinClient, String requestScript, Dictionary`2 bindings)

代码:

    private static string database = "db";
    private static string collection = "col";                                             
    private static string hostname = "grem-test.gremlin.cosmosdb.azure.com";
    public void test()
    {
        var gremlinServer = new GremlinServer(hostname, 443, enableSsl: true,
                                                username: "/dbs/" + database + "/colls/" + collection,
                                                password: authKey);
        var gremlinClient = new GremlinClient(gremlinServer);
        var grem = "g.V()";
        var t = gremlinClient.SubmitAsync<dynamic>(grem);
        t.Wait();

        foreach (var result in t.Result)
        {
            // The vertex results are formed as dictionaries with a nested dictionary for their properties
            string output = JsonConvert.SerializeObject(result);
            Console.WriteLine(String.Format("\tResult:\n\t{0}", output));
        }

2 个答案:

答案 0 :(得分:0)

应该是:

    var task = gremlinClient.SubmitAsync<dynamic>(grem);
    task.Wait();

摘自Gremlin C#示例:

                 // Create async task to execute the Gremlin query.
                var task = gremlinClient.SubmitAsync<dynamic>(query.Value);
                task.Wait();

答案 1 :(得分:0)

我从使用示例应用程序开始:

        private static Task<ResultSet<dynamic>> SubmitRequest(GremlinClient gremlinClient, string query)
        {
            try
            {
                return gremlinClient.SubmitAsync<dynamic>(query);
            }
            catch (ResponseException e)
            {
                // They have extra stuff here for the request information that isn't relevant
                throw;
            }
        }

我从那里扩展了,除了偶尔在尝试另一个查询仍在运行时运行查询的例外情况之外,没有任何问题。我只能假设以这种方式运行查询比直接调用SubmitAsync()更好。

我还建议另一件事是仔细检查服务器参数的值,以防万一。