首页 > 解决方案 > CosmosDB C# 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));
        }

标签: azure-cosmosdbgremlin

解决方案


它应该是:

    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();

推荐阅读