question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] Creating custom client with AddClient() in AddAzureClients() using ClientSecretCredential and ClientOptions

See original GitHub issue

Library name and version

Azure.Identity 1.9.0

Query/Question

What is the correct approach when calling a daemon when using a custom client. I would like to use the Azure.Identity package and use it’s client builder. Because this will manage the lifetime of the token.

The thing is that the AddClient method needs a class that inherits ClientOptions, but what purpose does it have? In this use case. Or is there an alternative?

A few observations:

  • If I provide a custom class it must inherit ClientOptions and must be registered in the DI,
  • The AddClient TOptions does not enforce it to be inherited by ClientOptions It will fail when not inherited with ClientOptions . (Constructor on type ‘MyCustomOptions’ not found.)
  • I could also use SecretClientOptions, which works, but it does not feel ok, because it could have side effects.

I use MyTokenKeeper in the DelegatingHandler of a HttpClient to fill the Authorization header. That’s the only thing I need in this use case.

            services.AddAzureClients(clientBuilder =>
            {
                clientBuilder.UseCredential(serviceProvider =>
                {
                    var myOptions = serviceProvider.GetRequiredService<myOptions>();
                    return new ClientSecretCredential(myOptions.TenantId, myOptions.ClientId, myOptions.Secret);
                });
                clientBuilder.AddClient<MyTokenKeeper, MyTokenKeeperClientOptions>((options, tokenCredential, serviceProvider) =>
                {
                    var myOptions = serviceProvider.GetRequiredService<MyOptions>();
                    var token = tokenCredential.GetToken(new TokenRequestContext(new[] { myOptions.Scope }), CancellationToken.None);
                    return new MyTokenKeeper(token.Token);
                });
            });

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jsquirecommented, Jul 31, 2023

Hi @spiral-dev. Thanks for reaching out and we regret that you’re experiencing difficulties. Your observations are accurate - the AzureClientFactoryBuilder is intended to build Azure SDK clients and assumes the conventions that they must use. It is not intended to be general purpose DI infrastructure, though there’s intentionally nothing that prevents you from using it that way. In your scenario, you are constructing a type that is not an Azure SDK client and does not follow those conventions. As a result, there’s a misalignment between the what your token keeper needs and the signature of the method.

Can you help me understand why you’re using the Azure Client extensions if you only need to register a token? Is there a reason that you chose not to just create the token and register it with DI?

services.AddSingleton<ClientSecretCredential>(serviceProvider=>
{
    var myOptions = serviceProvider.GetRequiredService<myOptions>();
    return new ClientSecretCredential(myOptions.TenantId, myOptions.ClientId, myOptions.Secret);
});

To answer your specific question - there is no purpose to the ClientOptions in your scenario. If you’d like to continue using the Azure client intrastructure, I’d suggest just asking for an Azure client options type and ignoring it. There’s no potential side-effects for having an options type created - they’re just simple data transfer models. In this scenario, ClientSecretCredentialOptions would seem to be reasonable, as you mentioned.

clientBuilder.AddClient<MyTokenKeeper, ClientSecretCredentialOptions>((_, tokenCredential, serviceProvider) =>
{
    var myOptions = serviceProvider.GetRequiredService<MyOptions>();
    var token = tokenCredential.GetToken(new TokenRequestContext(new[] { myOptions.Scope }), CancellationToken.None);
    return new MyTokenKeeper(token.Token);
});
0reactions
github-actions[bot]commented, Aug 9, 2023

Hi @spiral-dev, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[QUERY] Creating custom client with AddClient() in ...
[QUERY] Creating custom client with AddClient() in AddAzureClients() using ClientSecretCredential and ClientOptions #29465. Sign in to view logs.
Read more >
Dependency injection with the Azure SDK for .NET
Learn how to use dependency injection with the Azure SDK for .NET client libraries.
Read more >
Azure client library integration for ASP.NET Core
Register clients. Make a call to AddAzureClients in your app's ConfigureServices method. You can use the provided builder to register client ...
Read more >
Azure client library integration for ASP.NET Core
Core provides shared primitives to integrate Azure clients with ASP. ... Make a call to AddAzureClients in your app's ConfigureServices method. You can...
Read more >
Working with Azure SDK for .NET - Kaylumah
There are a few possible options to create resources in Azure: ... async Task Test_Scenario01_UsePrimaryConnectionString() { await using var ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found