FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

DI - Cannot resolve scoped service 'FluentEmail.Core.Interfaces.ISender' from root provider.

Open doug62 opened this issue 5 years ago • 2 comments

In CORE 3.1, when i register - services.AddFluentEmail("[email protected]") and later try to resolve by:

var fact = this.serviceProvider.GetRequiredService<FluentEmail.Core.IFluentEmailFactory>(); var email = fact.Create()

I receive: Cannot resolve scoped service 'FluentEmail.Core.Interfaces.ISender' from root provider.

Something in: https://github.com/lukencode/FluentEmail/blob/master/src/FluentEmail.Core/FluentEmailServiceCollectionExtensions.cs

Thanks.

doug62 avatar Apr 16 '20 06:04 doug62

This happened to me as well when I tried to resolve IFluentEmail from inside an IHostedService which doesn't automatically provide a scope. Fortunately you can create one very easily:

using (var scope = this.serviceProvider.CreateScope())
{
    var fact = scope.GetRequiredService<FluentEmail.Core.IFluentEmailFactory>();
    var email = fact.Create()
}

belidzs avatar May 23 '20 20:05 belidzs

Same problem with SendGrid and IHostedService.

https://github.com/lukencode/FluentEmail/blob/master/src/Senders/FluentEmail.SendGrid/FluentEmailSendGridBuilderExtensions.cs

You can see it registeres a singleton. Should work fine with IHostedService. What is the issue? TryAdd?

VictorioBerra avatar Jan 07 '21 21:01 VictorioBerra