Skip to content

Commit 72448bd

Browse files
committed
DI: Example, usage notes
1 parent e2b2d30 commit 72448bd

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Elasticstretch.DependencyInjection\Elasticstretch.DependencyInjection.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="*" />
17+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="*" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Elastic.Clients.Elasticsearch;
2+
using Elastic.Clients.Elasticsearch.Options;
3+
using Elastic.Clients.Elasticsearch.Serialization;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
var config = new ConfigurationBuilder()
8+
.AddJsonFile("appsettings.json")
9+
.Build();
10+
11+
await using var provider = new ServiceCollection()
12+
.AddSingleton<IConfiguration>(config)
13+
.AddElasticsearchClient()
14+
.Configure<ElasticsearchClientOptions>(
15+
options =>
16+
{
17+
options.ConfigureSettings += settings => settings.ThrowExceptions();
18+
options.SourceSerializer =
19+
settings => new DefaultSourceSerializer(settings, x => x.WriteIndented = true);
20+
})
21+
.BuildServiceProvider();
22+
23+
var client = provider.GetRequiredService<ElasticsearchClient>();
24+
25+
Console.WriteLine("Node pool is an {0}", client.ElasticsearchClientSettings.NodePool.GetType());
26+
27+
foreach (var node in client.ElasticsearchClientSettings.NodePool.Nodes)
28+
{
29+
Console.WriteLine("Node configured: {0}", node.Uri);
30+
}
31+
32+
if (client.ElasticsearchClientSettings.Authentication.TryGetAuthorizationParameters(out var credentials))
33+
{
34+
Console.WriteLine(
35+
"Credentials: {0} {1}",
36+
client.ElasticsearchClientSettings.Authentication.AuthScheme,
37+
credentials);
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"Elasticsearch": {
3+
"NodeUris": [
4+
"https://node1.example.com:9200",
5+
"https://node2.example.com:9200",
6+
"https://node3.example.com:9200",
7+
"https://node4.example.com:9200",
8+
"https://node5.example.com:9200"
9+
],
10+
"Credentials": {
11+
"ApiKeyId": "elasticstretch",
12+
"ApiKey": "abcdef12345"
13+
},
14+
"UseSniffing": true,
15+
"Randomize": true
16+
}
17+
}

Elasticstretch.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elasticstretch.DependencyInjection", "Elasticstretch.DependencyInjection\Elasticstretch.DependencyInjection.csproj", "{9485BE92-BCD7-46ED-9821-7603F2EB7676}"
1616
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elasticstretch.DependencyInjection.Example", "Elasticstretch.DependencyInjection.Example\Elasticstretch.DependencyInjection.Example.csproj", "{040A152A-AF74-4CBC-ABEE-3C8D65B3C3A8}"
18+
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1921
Debug|Any CPU = Debug|Any CPU
@@ -24,6 +26,10 @@ Global
2426
{9485BE92-BCD7-46ED-9821-7603F2EB7676}.Debug|Any CPU.Build.0 = Debug|Any CPU
2527
{9485BE92-BCD7-46ED-9821-7603F2EB7676}.Release|Any CPU.ActiveCfg = Release|Any CPU
2628
{9485BE92-BCD7-46ED-9821-7603F2EB7676}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{040A152A-AF74-4CBC-ABEE-3C8D65B3C3A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{040A152A-AF74-4CBC-ABEE-3C8D65B3C3A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{040A152A-AF74-4CBC-ABEE-3C8D65B3C3A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{040A152A-AF74-4CBC-ABEE-3C8D65B3C3A8}.Release|Any CPU.Build.0 = Release|Any CPU
2733
EndGlobalSection
2834
GlobalSection(SolutionProperties) = preSolution
2935
HideSolutionNode = FALSE

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
# Elasticstretch Extensions
22
Extensions for the [Elasticsearch .NET client](https://github.com/elastic/elasticsearch-net).
3+
4+
### Features
5+
* Inject/resolve Elasticsearch clients using `Microsoft.Extensions.DependencyInjection`.
6+
* Configure Elasticsearch clients using `Microsoft.Extensions.Options`.
7+
* Load config properties using `Microsoft.Extensions.Configuration`.
8+
9+
## Installation
10+
11+
Add the NuGet package to your project:
12+
13+
$ dotnet add package Elasticstretch.DependencyInjection
14+
15+
## Usage
16+
17+
### Resolving clients
18+
19+
Elasticstretch Dependency Injection works out-of-the-box after registering with an `IServiceCollection`.
20+
21+
```csharp
22+
services.AddElasticsearchClient();
23+
services.AddSingleton<MyService>();
24+
```
25+
26+
Inject the Elasticsearch client via constructor.
27+
28+
```c#
29+
public MyService(ElasticsearchClient client)
30+
{
31+
// Client is a singleton managed by the container.
32+
Client = client;
33+
}
34+
```
35+
36+
### Configuring clients
37+
38+
Supported configuration properties are bound to the `Elasticsearch` section of .NET configuration providers, such as `appsettings.json`.
39+
40+
```json
41+
{
42+
"Elasticsearch": {
43+
"NodeUris": [
44+
"https://node1.example.com:9200",
45+
"https://node2.example.com:9200",
46+
"https://node3.example.com:9200",
47+
"https://node4.example.com:9200",
48+
"https://node5.example.com:9200"
49+
],
50+
"Credentials": {
51+
"ApiKeyId": "elasticstretch",
52+
"ApiKey": "abcdef12345"
53+
},
54+
"UseSniffing": true,
55+
"Randomize": true
56+
}
57+
}
58+
```
59+
60+
You can also leverage `ElasticsearchClientOptions` for additional customization, such as serialization.
61+
62+
```csharp
63+
services.Configure<ElasticsearchClientOptions>(
64+
options =>
65+
{
66+
options.ConfigureSettings += settings => settings.ThrowExceptions();
67+
options.SourceSerializer =
68+
settings => new DefaultSourceSerializer(settings, x => x.WriteIndented = true);
69+
})
70+
```

0 commit comments

Comments
 (0)