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

Can ContentstackClient support IHttpClientFactory? #46

Open
sstere opened this issue Aug 23, 2024 · 4 comments
Open

Can ContentstackClient support IHttpClientFactory? #46

sstere opened this issue Aug 23, 2024 · 4 comments

Comments

@sstere
Copy link

sstere commented Aug 23, 2024

Hi! Can you support IHttpClientFactory as described in here?

If I register the ContentstackClient as a Singleton I will have a long lived internal HttpClient that is created inside your ContentstackClient. It would be nice if this internal HttpClient can be passed from an IHttpClientFactory that handles the automatic "refresh" of HttpClientHandler and therefore addressing the DNS changes problem as quoted from the article in the link.

"Another issue that developers run into is when using a shared instance of HttpClient in long-running processes. In a situation where the HttpClient is instantiated as a singleton or a static object, it fails to handle the DNS changes as described in this issue of the dotnet/runtime GitHub repository."

@nadeem-cs @cs-raj You can make ContentstackClient receive HttpClient as constructor parameter and on a static extension method from some static class you could have something as:

public static IServiceCollection AddContentstackClientService(this IServiceCollection services)
{ 
   services.AddHttpClient<ContentstackClient>(client =>
{
    client.Timeout = contentstackOptions.Timeout;
    client.MaxResponseContentBufferSize = contentstackOptions.MaxResponseContentBufferSize;
    client.DefaultRequestHeaders.Add("X-User-Agent", xUserAgent ?? "");

    if (!client.DefaultRequestHeaders.UserAgent.Any())
    {
        client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("contentstack-management-dotnet", Version));
        client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("DotNet", System.Environment.Version.ToString()));
    }
}).ConfigurePrimaryHttpMessageHandler(() =>
   {
       return new HttpClientHandler
       {
           Proxy = contentstackOptions.GetWebProxy()
       };
   });
}

Then I could just call services.AddContentstackClientService() in my startup and inject ContentstackClient wherever I need it knowing that the HttpClient is now managed by the IHttpClientFactory.

What do you think?

Thanks!

@sstere
Copy link
Author

sstere commented Aug 23, 2024

Mentioning @nadeem-cs, @cs-raj and @uttamukkoji

@sstere sstere changed the title Can ContentstackClient support IHttpClientFactory pattern? Can ContentstackClient support IHttpClientFactory? Aug 23, 2024
@cs-raj
Copy link
Contributor

cs-raj commented Oct 24, 2024

Hi @sstere we have created ticket for the same, and we will be handling in upcoming sprints

@sstere
Copy link
Author

sstere commented Nov 6, 2024

@cs-raj Please also check issue #18 is indeed fixed. When I used ContentstackClient as Singleton I had same problem with that exception even if I upgraded to the latest version that contains the fix (0.1.8). I've seen now that is 0.1.10.

Is "Request Header Fields Too Large" really fixed now?

@sstere
Copy link
Author

sstere commented Nov 6, 2024

@cs-raj @nadeem-cs This client SDK is used by a big customer of Contentstack and I preferred to signal the issues in a public way and not go through the paid corporate support channel.

I hope you guys improve the quality of this library and make it more reliable.

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

No branches or pull requests

2 participants