C#C
C#2y ago
hutonahill

✅ Identity Services Registration not working

I declare a class to handle 'email':
public class MyEmailServices<TUser> : IEmailSender {
    private readonly string _logFilePath = "fakeMail.txt";

    public MyEmailServices() {
        
    }

    public async Task SendEmailAsync(string email, string subject, string message)
    {
        // Format the log entry
        string logEntry = $"[{DateTime.Now}] To: {email}\nSubject: {subject}\nMessage: {message}\n\n";
        
        // Append the log entry to the file
        await File.AppendAllTextAsync(_logFilePath, logEntry);
    }
}


Then in Program.cs I register the service:
builder.Services.AddTransient<IEmailSender, MyEmailServices<IdentityUser>>();


Finally i try and grab the service and it throws an error
var emailSender = endpoints.ServiceProvider.GetRequiredService<IEmailSender<TUser>>();

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.IEmailSender`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.


Everywhere i look it people use this same method to register the emailsender, so i am not sure what i am doing wrong.
Was this page helpful?