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

Deprecation notice: intent to remove ShopifySharp's DefaultHttpClientFactory #899

Open
nozzlegear opened this issue Jul 13, 2023 · 0 comments

Comments

@nozzlegear
Copy link
Owner

nozzlegear commented Jul 13, 2023

I've just marked ShopifySharp.Infrastructure.DefaultHttpClientFactory as obsolete in v6.2, and intend to remove it in a future release after sufficient time has passed. Inside ShopifySharp, I've replaced the class with a new InternalHttpClientFactory which, as the name implies, is internal access only.

The issue with the DefaultHttpClientFactory is that it ignores all configuration passed to it and returns a completely unmodified HttpClient:

  public class DefaultHttpClientFactory : IHttpClientFactory
  {
      private static readonly HttpClient _Client = new HttpClient();

      public HttpClient CreateClient(string name)
      {
          return _Client;
      }
  }

This is fine for ShopifySharp's own internal usage, it doesn't need or expect the factory to do any kind of configuration. However, if you're like me and decide that ShopifySharp conveniently gives you this nice, public IHttpClientFactory that you can just plug into your Startup.cs file and then forget about, you'll start to run into bugs when other services in your app expect the factory to configure the client but it doesn't.

Consider the following:

public static void AddFooHttpClient(this IServiceCollection services)
{
  services.AddHttpClient("FooClient")
    .ConfigureHttpClient((serviceProvider, client) =>
    {
      client.BaseAddress = new Uri("https://example.com/api/v1/");
      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("...");
    });
}

This method is clearly expecting the factory to call its configuration method so that it can configure the HttpClient with that base address and default authorization header. But since ShopifySharp dutifully ignores the configuration, those defaults are never set and whatever the "FooClient" is used for is going to break.

If you accidentally used ShopifySharp's DefaultHttpClientFactory in your app's DI services like I did, you can replace it with a simple services.AddHttpClient() from Microsoft.Extensions.DependencyInjection

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

No branches or pull requests

1 participant