-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add EventHub client extension methodS #7034
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core.Pipeline; | ||
|
||
namespace Azure.Core.Extensions | ||
{ | ||
|
||
public interface IAzureClientFactoryBuilderWithConfiguration<in TConfiguration>: IAzureClientFactoryBuilder | ||
{ | ||
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(TConfiguration configuration) where TOptions : ClientOptions; | ||
IAzureClientBuilder<TClient, TOptions> RegisterClientFactory<TClient, TOptions>(TConfiguration configuration) where TOptions : class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core.Extensions; | ||
using Azure.Messaging.EventHubs; | ||
|
||
namespace Azure.ApplicationModel.Configuration | ||
{ | ||
/// <summary> | ||
/// Extension methods to add <see cref="EventHubClient"/> client to clients builder | ||
/// </summary> | ||
public static class AzureClientBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="connectionString"/> | ||
/// </summary> | ||
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder>(this TBuilder builder, string connectionString) | ||
where TBuilder: IAzureClientFactoryBuilder | ||
{ | ||
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(options => new EventHubClient(connectionString, options)); | ||
} | ||
|
||
/// <summary> | ||
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="connectionString"/> and <paramref name="eventHubPath"/> | ||
/// </summary> | ||
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder>(this TBuilder builder, string connectionString, string eventHubPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may not have context enough to be reading this entirely correctly, but I believe this limits scenarios to:
If that is the case, I'd suggest that we also allow:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So to add a client and configure its options: builder.AddEventHubClient("connectionString", "path").ConfigureOptions(options => {} ); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Works for me! |
||
where TBuilder: IAzureClientFactoryBuilder | ||
{ | ||
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(options => new EventHubClient(connectionString, eventHubPath, options)); | ||
} | ||
|
||
/// <summary> | ||
/// Registers a <see cref="EventHubClient"/> instance with the provided <paramref name="host"/> and <paramref name="eventHubPath"/> | ||
/// </summary> | ||
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClientWithHost<TBuilder>(this TBuilder builder, string host, string eventHubPath) | ||
where TBuilder: IAzureClientFactoryBuilderWithCredential | ||
{ | ||
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>((options, token) => new EventHubClient(host, eventHubPath, token, options)); | ||
} | ||
|
||
/// <summary> | ||
/// Registers a <see cref="EventHubClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance. | ||
/// </summary> | ||
public static IAzureClientBuilder<EventHubClient, EventHubClientOptions> AddEventHubClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration) | ||
where TBuilder: IAzureClientFactoryBuilderWithConfiguration<TConfiguration> | ||
{ | ||
return builder.RegisterClientFactory<EventHubClient, EventHubClientOptions>(configuration); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider making the options always have a default constructor and using that constraint, or is that unimportant down the line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, they almost never have a default constructor. Instead, the guidelines prescribe having a constructor with ServiceVersion parameter defaulted to latest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. Forgot about that due to the deviation in the Event Hubs service design.