-
Notifications
You must be signed in to change notification settings - Fork 570
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
Comments
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. |
Good to hear from you, @pakrym-stripe .
Yes, I figure it would be. But I find it odd that none of Stripe's samples do it that way. |
Hey, have you seen some ASP.NET Core-specific samples that do that? Or do you mean the general-purpose samples? |
I only follow Asp.Net Core samples since that's what I use on the backend. |
Can you link to the sample you are following? I'll try to see if we can make it better. |
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. |
In This said, it should be recommended that for long-running services; it should create a transient instance of In pre- |
@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);
}); |
@cecilphillip-stripe, looks great! |
@HappyNomad Is this resolved for you? |
Closing as the original question is answered. The general DI support can be discussed in #1882 |
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
inStartup.cs
has:I've thoroughly searched but can't find the answer to this basic question. I hope I get one here.
The text was updated successfully, but these errors were encountered: