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

Dependency Injection in Startup.cs #880

Closed
KevinMallinson opened this issue May 3, 2019 · 7 comments · Fixed by #922
Closed

Dependency Injection in Startup.cs #880

KevinMallinson opened this issue May 3, 2019 · 7 comments · Fixed by #922
Labels
difficulty: hard fix is hard in difficulty type: community enhancement feature request not on Twilio's roadmap

Comments

@KevinMallinson
Copy link

Hi, sorry for not following the template, but regarding issues #847 and #521, I was wondering if there is any progress?

I would like to inject a SendGridClient through DI to a controller, instead of instantiating a new client every request.

I can't rely on services.AddHttpClient<ISendGridClient, SendGridClient>(); because I need to add the Key that is typically called in the SendGridClient constructor.

Is this workflow being looked in to?

Thanks

@thinkingserious thinkingserious added difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels May 3, 2019
@thinkingserious
Copy link
Contributor

Hello @KevinMallinson,

I don't know the answer to this offhand, I will need to investigate further.

In the mean time, could you please elaborate on what the behavior you are seeking would look like in code if we had already implemented it so that I may better understand your workflow?

With Best Regards,

Elmer

@KevinMallinson
Copy link
Author

KevinMallinson commented May 4, 2019

The behaviour would look something like this (psuedocode):

MySendGridClient.cs

public class MySendGridClient : ISendGridClient
{
    // Concrete implementation that we forward our calls to
    private SendGridClient _myClient;

    public class MySendGridClient(HttpClient httpClient, string apiKey)
    {
        //Add custom headers, etc
        // ...

        _myClient = new SendGridClient(apiKey, httpClient);
    }

    // Below this line, each method of the interface is implemented, 
    // simply by forwarding to our internal sendgridclient.

    public Task SomeMethod() => _myClient.SomeMethod();
    // etc.
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    services.AddHttpClient<ISendGridClient, MySendGridClient>();
}

MyController.cs

public class MyController
{
    public MyController(ISendGridClient myClient)
    {
        // myClient is passed via dependency injection
        // it is always instantiated, no need to re-instantiate each time
        // Care needs to be taken to ensure concurrency and thread safety
    }
}

Hopefully that makes sense,

Thanks.

Edit: Changed the example to look more like the Twilio example

@thinkingserious thinkingserious added difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: enhancement and removed difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels May 8, 2019
@thinkingserious
Copy link
Contributor

Thank you @KevinMallinson,

Your description was very helpful, thanks for taking the time!

I've added this to our internal backlog for consideration as well as opened it up for community contribution.

With Best Regards,

Elmer

@aevitas
Copy link
Contributor

aevitas commented Jun 23, 2019

This could be added relatively easily by adding a constructor that accepts an IOptions<SendGridClientOptions> argument, unwraps the option type, and calls the existing constructor with the SendGridClientOptions options signature.

In your DI configuration, you'd do something along the lines of:

services.Configure<SendGridClientOptions>(Configuration.GetSection("SendGrid");
services.AddScoped<ISendGridClient, SendGridClient>();

Let me know if you want me to apply these changes, I'll happily implement them and submit a PR.

@thinkingserious
Copy link
Contributor

Hello @aevitas,

A PR would be most welcome. Thank you!

With best regards,

Elmer

@KevinMallinson
Copy link
Author

@aevitas @thinkingserious

#839 was merged, will #904 require changes, and is this still planned on being merged?

@masaab
Copy link

masaab commented Aug 26, 2020

You can use Install-Package SendGrid.Extensions.DependencyInjection -Version 1.0.0
And use it in your startup Configure method. Something like this.

builder.Services.AddSendGrid(options => {
options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");

@childish-sambino childish-sambino removed status: help wanted requesting help from the community up for grabs labels Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: hard fix is hard in difficulty type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants