Skip to content
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

CreateClient<TClient> Naming Issue #4

Closed
gorillapower opened this issue Nov 15, 2024 · 2 comments · Fixed by #5
Closed

CreateClient<TClient> Naming Issue #4

gorillapower opened this issue Nov 15, 2024 · 2 comments · Fixed by #5
Assignees

Comments

@gorillapower
Copy link

gorillapower commented Nov 15, 2024

I encountered an issue when my Typed HttpClient was using a generic type. In my example .AddHttpClient<GenericClient<MyName>> produced "GenericClient`1" in the IContextualHttpClientFactory.CreateClient<TClient>(string handlerContext)) method

image

This caused issues as I was expecting the name to be GenericClient<MyName>. It looks like in .NET there is an internal method that resolves the TClient name string. See this code string name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);

I resolved the issue by creating my own IContextualHttpClientFactory and creating the CreateClient to look like this

    public TClient CreateClient<TClient>(string handlerContext)
    {
        var client = CreateClient(typeof(TClient).FullName, handlerContext);
        return _serviceProvider.GetRequiredService<ITypedHttpClientFactory<TClient>>().CreateClient(client);
    }

This resolved the FullName with the generic type included.

UPDATE: This solution did not work, the TClient needs to resemble the exact name that the internal AddHttpClient would create, being GenericClient<MyName>.

What did end up working was to replicate the TypeNameHelper.GetTypeDisplayName() code referenced above. Working Example.

    public TClient CreateClient<TClient>(string handlerContext)
    {
         string name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
         var client = CreateClient(name, handlerContext);
        return _serviceProvider.GetRequiredService<ITypedHttpClientFactory<TClient>>().CreateClient(client);
    }
@agertenbach agertenbach self-assigned this Nov 15, 2024
@agertenbach
Copy link
Owner

Thanks for the report, issue confirmed.

I have a pending branch and PR to address this issue along with additions to properly handle typed clients with interfaces and some other small quality of life updates.

@agertenbach agertenbach linked a pull request Nov 18, 2024 that will close this issue
@agertenbach
Copy link
Owner

3.0.0-rc is available here and as a pre-release package on Nuget. It has a major version increment due to some small changes on interfaces.

The issue you reported is addressed by a new wrapper type called TypedClientSignature that uses an internal implementation of TypeNameHelper and has implicit conversion to and from a string to maintain existing comparison logic.

In addition to resolving issues with typed clients with a generic parameter, the handler factory logic now supports

client.IsTypedClient<TypedClient>()

in favor of (but optionally in addition to the potentially inaccurate)

client == typeof(TypedClient).Name

Let me know if you have any issues with the RC, will plan to merge this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants