Skip to content

Commit cf000f2

Browse files
committed
[Docs] Add section on subclassing connections
1 parent 238d399 commit cf000f2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: docs/contents/nest/connecting.markdown

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ By default NEST will use HTTP to chat with Elasticsearch, alternative implementa
3737
NEST comes with an Http Connection `HttpConnection`, Thrift Connection `ThriftConnection`
3838
and an In-Memory Connection `InMemoryConnection`, that nevers hits Elasticsearch.
3939

40+
You can also roll your own connection if desired by implementing the `IConnection` interface.
41+
42+
## Subclassing existing connection implementations
43+
44+
In addition to implementing your own `IConnection`, the existing `HttpConnection` and `ThriftConnection` are extendible and can be subclassed in order to modify or extend their behavior.
45+
46+
For instance, a common use case is the ability to add client certificates to web requests. You can subclass `HttpConnection` and override the `CreateHttpWebRequest` method that creates the web request, and add certificates to it like so:
47+
48+
public class SignedHttpConnection : HttpConnection
49+
{
50+
private readonly X509CertificateCollection _certificates;
51+
52+
public SignedHttpConnection(IConnectionConfigurationValues settings, X509CertificateCollection certificates)
53+
: base(settings)
54+
{
55+
_certificates = certificates;
56+
}
57+
58+
protected override HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConfiguration requestSpecificConfig)
59+
{
60+
var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
61+
request.ClientCertificates = _certificates;
62+
return request;
63+
}
64+
}
65+
4066
## Settings
4167

4268
The `NEST` client can be configured by passing in an `IConnectionSettingsValues` object, which is a sub-interface of

0 commit comments

Comments
 (0)