DI - Cannot resolve scoped service 'FluentEmail.Core.Interfaces.ISender' from root provider.
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.
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()
}
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?