Skip to content

Conversation

@maartenba
Copy link

Fixes #377

this FluentEmailServicesBuilder builder,
string serverToken)
{
builder.Services.TryAdd(ServiceDescriptor.Scoped<ISender>(_ => new MailPaceSender(serverToken)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason to not bind this in Singleton scope? If there are a large number of concurrent requests couldn't you get resource exhaustion due to all the HttpClients that are created in MailPaceSender's constructor?

Microsoft indicates either HttpClient should be created in singleton scope or use IHttpClientFactory. https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! Singleton makes more sense here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one caveat with singleton on this class is not being able to set the PooledConnectionLifetime on the internal HttpClient itself. Binding in Singleton scope would help with resource exhaustion but there could be a potential issue with long lived connections. Changes to DNS records may not be picked up. It might be better to manage the HttpClient lifecycle inside MailGunSender and MailPaceSender and set the PooledConnectionLifetime to some reasonable default. Then it won't matter how ISender is bound.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would passing in IHttpClientFactory be a better option? Let the DI container handle HttpClient lifetimes?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think that should work too.

I had a play around with this for MailGunSender in #382

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 this pull request may close these issues.

Would you be interested in a MailPace mail provider integration?

2 participants