弹性搜索Nest .net客户端挂起任何请求。

时间:2016-01-05 00:31:05

标签: .net elasticsearch nest

永久阻止,不会返回或发布连接状态消息。这是一个代码片段,显示了我如何连接到默认端口。使用Chrome浏览器验证了连接。

    static void ConnectToElasticIndex(string name)
    {
        if (String.IsNullOrEmpty (name))
            throw(new ArgumentNullException ("name"));

        var node = new Uri ("http://localhost:9200");

        var settings = new ConnectionSettings (
            node, 
            name
        ).SetConnectionStatusHandler(h=>
            _log.InfoFormat("Url: {0} | Method: {1} | Request: \n{2}",
            h.RequestUrl,
            h.RequestMethod,
                Encoding.UTF8.GetString(h.Request)));

        _client = new ElasticClient (settings);

阻止此处或我对客户的任何通话。

        var res = _client.Raw.ClusterHealth();

        _log.Info(res.SuccessOrKnownError);
    }

2 个答案:

答案 0 :(得分:0)

问题在于这行代码

Encoding.UTF8.GetString(h.Request)

Request可以为null,例如在ClusterHealth()调用的情况下,当发生这种情况时,该行将抛出异常。我不确定当你说阻止时你会看到什么行为,但你上面的内容会抛出一个空引用异常。

如果您想记录请求,那么以下内容适用于具有正文的请求以及不具有<* p>的请求

var settings = new ConnectionSettings(
    node,
    name
)
.SetConnectionStatusHandler(response =>
{
    // log out the requests
    if (response.Request != null)
    {
        Console.WriteLine("{0} {1} \n{2}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl,
            Encoding.UTF8.GetString(response.Request));
    }
    else
    {
        Console.WriteLine("{0} {1}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl);
    }
});

如果您还想注销回复,可以使用

var settings = new ConnectionSettings(
    node,
    name
)
.ExposeRawResponse(true)
.SetConnectionStatusHandler(response =>
{
    // log out the requests
    if (response.Request != null)
    {
        Console.WriteLine("{0} {1} \n{2}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl,
            Encoding.UTF8.GetString(response.Request));
    }
    else
    {
        Console.WriteLine("{0} {1}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl);
    }

    if (response.ResponseRaw != null)
    {
        Console.WriteLine("Status: {0}\n{1}\n\n{2}\n", response.HttpStatusCode, Encoding.UTF8.GetString(response.ResponseRaw), new String('-', 30));
    }
    else
    {
        Console.WriteLine("Status: {0}\n\n{1}\n", response.HttpStatusCode, new String('-', 30));
    }
});

答案 1 :(得分:0)

很奇怪。它现在没有阻止。它必须与OS TCP / IP相关。这一次,我重新启动了计算和弹性节点以及VOILA!它有效。