0
votes

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.

1

1 Answers

0
votes

Just as I finished writing the question, it dawned on me that I didn't update my blazor components to inject the interface version of the service as well.

So in a component which used the EmployeeService, it looked like this

[Inject] EmployeeService EmployeeServ { get; set; }

And once I changed it to this, it then worked

[Inject] IEmployeeService EmployeeServ { get; set; }