Skip to content

SendgridClient constructor issue #670

@vlmironoff

Description

@vlmironoff

I'm trying to provide a DI controlled HttpClient object (singleton) to the constructor of SendgridClient to overcome known socket exhausting issue by reusing the instance of the aforementioned HttpClient.
The first email sends just fine, however the second one fails with this exception:

InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request.

Having done additional research I figured that a single instance of HttpClient allows changing its configuration properties (BaseAddress in particular) only before the 1st connection is made with this instance. Trying to modify BaseAddress property after HttpClient was used causes this exception to be thrown.
Referenced SO post:
https://stackoverflow.com/questions/42235677/httpclient-this-instance-has-already-started

I went to the source code of SendgridClient and found out that the constructor of SendGridClient calls InitiateClient function which resets the BaseAddress of the underlying HttpClient object every time the SengridClient instance is created:
https://github.com/sendgrid/sendgrid-csharp/blob/master/src/SendGrid/SendGridClient.cs
Line: 157

On one hand one can register an instance of SendgridClient in a DI container, but it defies the intended purpose of having such constructor overload in SendgridClient class, allowing to provide the existing instance of HttpClient.

One way to fix it could be checking if the provided instance of HttpClient already has its params configured and not overwrite them if so.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions