© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
13 replies
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);
    }
}
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>>();
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>>();
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.
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ login registration form not working
C#CC# / help
3y ago
OAuth not working with Identity
C#CC# / help
3y ago
❔ Services Not Working with DB Model
C#CC# / help
3y ago
DI: Registration of services through a builder.
C#CC# / help
2y ago