1
votes

I am using asp.net boilerplate to create new project.

I have defined new service as follows:

public class Employee : Entity<int>
{
    public string FName { get; set; }
    public string LName { get; set; }
}

public interface IEmployeeAppService : IApplicationService
{
    Employee AddEmployee(Employee emp);
    List<Employee> GetAll();
}

public class EmployeeAppService : MyTestProjectAppServiceBase, IEmployeeAppService
{
    private IRepository<Employee, int> _employeeRepository;

    public EmployeeAppService(IRepository<Employee, int> repo)
    {
        _employeeRepository = repo;
    }

    public Employee AddEmployee(Employee emp)
    {
        return _employeeRepository.Insert(emp);
    }

    public List<Employee> GetAll()
    {
        return _employeeRepository.GetAllList();
    } 
}

I want to use the service in HomeController:

public class HomeController : MyTestProjectControllerBase
{
    IEmployeeAppService service;

    public HomeController(IEmployeeAppService svc)
    {
        service = svc;
    }
}

When I run the application I get following error:

Can't create component 'MyTestProject.Services.EmployeeAppService' as it has dependencies to be satisfied.

'MyTestProject.Services.EmployeeAppService' is waiting for the following dependencies:
- Service 'Abp.Domain.Repositories.IRepository`2[[MyTestProject.Domain.Employee, MyTestProject.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' which was not registered.

How do I register the EmployeeAppService dependency with HomeController?

UPDATE

I tried the following code

IocManager.Register(typeof(IRepository<Employee, int>),
                    typeof(EmployeeAppService),
                    Abp.Dependency.DependencyLifeStyle.Transient);

but then it displays this error

There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.

2
Where/How do you register your dependencies, or have you not got DI working at all yet? - Jamiec
not got DI working, this is my first service which I want to register - Nitin S
IocManager.RegisterAssemblyByConvention auto registers dependancies, I wonder why its not taking my new service - Nitin S
Also, your controllers should inject interfaces in the constructor not concrete classes (more likely to be your problem). - Jamiec

2 Answers

9
votes

This normally happens when your Entity (Employee in this case) is not specified in you DbContext.

Just add the following property to your DbContext class and you should be good to go:

public virtual IDbSet<Employee> Employees { get; set; }

1
votes

You don't need to register application service manually. Remove the code IocManager.Register ...

When you base from IApplicationService it's automatically registered to DI.

Error says EmployeeAppService cannot be resolved. So just like Jacques Snyman said, add the Employee entity to DbContext.