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

StripeClient instance per controller vs. singleton #2417

Closed
HappyNomad opened this issue Jan 25, 2022 · 11 comments
Closed

StripeClient instance per controller vs. singleton #2417

HappyNomad opened this issue Jan 25, 2022 · 11 comments

Comments

@HappyNomad
Copy link

I'm starting my first Stripe integration and need to know which of the following mutually exclusive alternatives, for Asp.Net Core, is the preferred approach.

Option A

Controller constructors have new StripeClient(...

Option B

ConfigureServices in Startup.cs has:

services.AddSingleton<IStripeClient, StripeClient>(s => new StripeClient(

I've thoroughly searched but can't find the answer to this basic question. I hope I get one here.

@pakrym-stripe
Copy link
Contributor

Hey, @HappyNomad, sorry for the long response. Option B will be more performant and scale better because you are avoiding creating new HttpClient for every request.

@HappyNomad
Copy link
Author

Good to hear from you, @pakrym-stripe .

Option B will be more performant and scale better because you are avoiding creating new HttpClient for every request.

Yes, I figure it would be. But I find it odd that none of Stripe's samples do it that way.

@pakrym-stripe
Copy link
Contributor

Hey, have you seen some ASP.NET Core-specific samples that do that? Or do you mean the general-purpose samples?

@HappyNomad
Copy link
Author

I only follow Asp.Net Core samples since that's what I use on the backend.

@pakrym-stripe
Copy link
Contributor

Can you link to the sample you are following? I'll try to see if we can make it better.

@HappyNomad
Copy link
Author

Thanks for your interest in improving the samples. I followed along with the Stripe.net playlist on Youtube. In particular, I watched and rewatched the episodes on setting up an Asp.Net Core project and accepting a payment.

@bobbyangers
Copy link

bobbyangers commented Apr 2, 2022

In Core; the lifetime of the HttpClient is managed; and one should rely on that. It's not exactly singleton, it's shared.

This said, it should be recommended that for long-running services; it should create a transient instance of IStripeClient and use the IHttpClientFactory to resolve the "current" instance of the HttpClient

In pre-Core a singleton is used.

@cecilphillip-stripe
Copy link

@pakrym-stripe what do you think about using something like this using the httpclient factory

   services.AddHttpClient("Stripe");
   services.AddTransient<IStripeClient, StripeClient>(s =>
   {
       var clientFactory = s.GetRequiredService<IHttpClientFactory>();
       var httpClient = new SystemNetHttpClient(
          httpClient: clientFactory.CreateClient("Stripe"),
          maxNetworkRetries: StripeConfiguration.MaxNetworkRetries,
          appInfo: appInfo,
          enableTelemetry: StripeConfiguration.EnableTelemetry);

        return new StripeClient(apiKey: StripeConfiguration.ApiKey, httpClient: httpClient);
   });

@pakrym-stripe
Copy link
Contributor

@cecilphillip-stripe, looks great!

@cecilphillip-stripe
Copy link

@HappyNomad Is this resolved for you?

@pakrym-stripe
Copy link
Contributor

Closing as the original question is answered. The general DI support can be discussed in #1882

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

4 participants