forked from dotnet/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHubConsumerClientComponent.cs
57 lines (49 loc) · 2.72 KB
/
EventHubConsumerClientComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Azure.Messaging.EventHubs;
using Azure.Core.Extensions;
using Azure.Messaging.EventHubs.Consumer;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Extensions.Hosting;
internal sealed class EventHubConsumerClientComponent : EventHubsComponent<AzureMessagingEventHubsConsumerSettings, EventHubConsumerClient, EventHubConsumerClientOptions>
{
// cannot be in base class as source generator chokes on generic placeholders
protected override void BindClientOptionsToConfiguration(IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClientOptions> clientBuilder, IConfiguration configuration)
{
#pragma warning disable IDE0200 // Remove unnecessary lambda expression - needed so the ConfigBinder Source Generator works
clientBuilder.ConfigureOptions(options => configuration.Bind(options));
#pragma warning restore IDE0200
}
protected override void BindSettingsToConfiguration(AzureMessagingEventHubsConsumerSettings settings, IConfiguration config)
{
config.Bind(settings);
}
protected override IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClientOptions> AddClient(
AzureClientFactoryBuilder azureFactoryBuilder, AzureMessagingEventHubsConsumerSettings settings,
string connectionName, string configurationSectionName)
{
return ((IAzureClientFactoryBuilderWithCredential)azureFactoryBuilder).RegisterClientFactory<EventHubConsumerClient, EventHubConsumerClientOptions>((options, cred) =>
{
EnsureConnectionStringOrNamespaceProvided(settings, connectionName, configurationSectionName);
var consumerGroup = settings.ConsumerGroup ?? EventHubConsumerClient.DefaultConsumerGroupName;
// If no connection is provided use TokenCredential
if (string.IsNullOrEmpty(settings.ConnectionString))
{
return new EventHubConsumerClient(consumerGroup, settings.FullyQualifiedNamespace, settings.EventHubName, cred, options);
}
else
{
// If no specific EventHubName is provided, it has to be in the connection string
if (string.IsNullOrEmpty(settings.EventHubName))
{
return new EventHubConsumerClient(consumerGroup, settings.ConnectionString, options);
}
else
{
return new EventHubConsumerClient(consumerGroup, settings.ConnectionString, settings.EventHubName, options);
}
}
}, requiresCredential: false);
}
}