标签:InMemoryConnection default NEST 03Modifying connection HttpConnection var new me
Modifying the default connection(修改默认连接) 客户端在IConnection之后抽象发送请求并创建响应,默认实现使用System.Net.Http.HttpClient。 您为什么要通过自己的IConnection?让我们看几个例子 Using InMemoryConnectionedit(使用 InMemoryConnectionedit) InMemoryConnection是一个内置的IConnection,可以轻松编写针对其的单元测试。可以将其配置为使用默认响应字节,HTTP状态代码和调用时的异常进行响应。 InMemoryConnection实际上并没有从Elasticsearch发送任何请求或接收任何响应;如果.DisableDirectStreaming在请求上或全局设置为true,则请求仍会序列化,并且可以在响应上获取请求字节var connection = new InMemoryConnection(); var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var settings = new ConnectionSettings(connectionPool, connection); var client = new ElasticClient(settings); |
var response = new { took = 1, timed_out = false, _shards = new { total = 2, successful = 2, failed = 0 }, hits = new { total = new { value = 25 }, max_score = 1.0, hits = Enumerable.Range(1, 25).Select(i => (object)new { _index = "project", _type = "project", _id = $"Project {i}", _score = 1.0, _source = new { name = $"Project {i}" } }).ToArray() } }; var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); var connection = new InMemoryConnection(responseBytes, 200); ① var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("project"); var client = new ElasticClient(settings); var searchResponse = client.Search<Project>(s => s.MatchAll()); |
public class MyCustomHttpConnection : HttpConnection { protected override HttpRequestMessage CreateRequestMessage(RequestData requestData) { var message = base.CreateRequestMessage(requestData); var header = string.Empty; message.Headers.Authorization = new AuthenticationHeaderValue("Negotiate", header); return message; } } public class KerberosConnection : HttpConnection { protected override HttpRequestMessage CreateRequestMessage(RequestData requestData) { var message = base.CreateRequestMessage(requestData); var header = string.Empty; message.Headers.Authorization = new AuthenticationHeaderValue("Negotiate", header); return message; } } |
标签:InMemoryConnection,default,NEST,03Modifying,connection,HttpConnection,var,new,me 来源: https://www.cnblogs.com/xianchengzhang/p/14629951.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。