Diogo Kobbi (5) [Avatar] Offline
#1
Hi, Andrew.

I'm sorry. I'm trying to understand how this works.

How the method AddEmailSender is added to services (IServiceCollection)? The .Net core DI does that?

I've got the services.addScoped inside the AddEmailSender, because you passing the services collection throught the method. But how the services on ConfigureServices can call the AddEmailSender method?

Thanks,
Andrew Lock (51) [Avatar] Offline
#2
Hi,

If I understand you correctly, then if you look at the snippet just under Listing 10.10 you can see that we call AddEmailSender in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddEmailSender();
}


Does that solve your problem?

Andrew
Diogo Kobbi (5) [Avatar] Offline
#3
Ok, i figured it out. It's a C# feature:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

Please, close this topic.
Diogo Kobbi (5) [Avatar] Offline
#4
Andrew Lock wrote:Hi,

If I understand you correctly, then if you look at the snippet just under Listing 10.10 you can see that we call AddEmailSender in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddEmailSender();
}


Does that solve your problem?

Andrew


Kind of. I figured it out that the AddEmailSender was added as extension method by the compiler (i think). I didnt knew about this feature before.

BTW, this chapter is the best explanation about the DI on the internet. Thank you so much.
Andrew Lock (51) [Avatar] Offline
#5
Ah I see, yes this is a feature of C# as you discovered smilie Sorry, I misunderstood the question!

Hopefully you've found this: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

Glad you've found it otherwise useful smilie

Andrew