I am updating a project to use interfaces.
I have an interface called IEmployeeService.
public interface IEmployeeService
{
List<Employee> GetEmployees();
}
I have a service which implements this interface. BUT the service also has a constructor
public class EmployeeService : IEmployeeService
{
private IDbConnection db;
public EmployeeService(string connString)
{
this.db = new SqlConnection(connString);
}
public List<Employee> GetEmployees(bool activeOnly = false)
{
// Do something
}
}
If I register the service in Startup.cs without mentioning the interface like so, then it works fine
services.AddTransient<EmployeeService>(_ => new EmployeeService(
Configuration.GetConnectionString("DefaultConnection")));
When I register it using the interface instead like so. I then get the error reported as seen below.
services.AddTransient<IEmployeeService>(_ => new EmployeeService(
Configuration.GetConnectionString("DefaultConnection")));
Error: System.InvalidOperationException: Cannot provide a value for property 'EmployeeServ' on type 'RolesAndSecurityMatrix.SSBlazorUI.Pages.SearchByEmployee'. There is no registered service of type 'RolesAndSecurityMatrix.DataAccess.EmployeeService'.
Note: this is a Blazor Server-side app and these services mentioned above.