0

Work on Aspnet core boilerplate framework stuck on one issue, form my controller failed to call services.

Application class library contains IEmployeeService and EmployeeService, how to call them from my EmployeeController.

Service

    public interface IEmployeeService
    {
        int CreateEmployee(CreateEmployeeDto data);
        IEnumerable<EmployeeListDto> GetEmployeeList();

    }
 public class EmployeeService : IEmployeeService
    {
}

Controller

    [AbpMvcAuthorize]
    public class EmployeeController : HRISControllerBase
    {


        private readonly IEmployeeService _employeeService;

        public EmployeeController(           
            IEmployeeService employeeService
           )
        {

            _employeeService = employeeService;           
        }

        public ActionResult Index()
        {
            return View();
        }
}

Note: Do project need to configure something in ConfigureServices on the Startup.cs file.

shamim
  • 6,640
  • 20
  • 85
  • 151

4 Answers4

1

You need register it in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IEmployeeService, EmployeeService>();
}
asd
  • 854
  • 7
  • 10
  • this process for .net core project DI want to know how to use DI on aspnetboilerplate asp.net-core project controller. – shamim Apr 29 '19 at 12:36
  • @shamim - this last comment demonstrates either a misunderstanding in how DI works, or the original question is ambiguous in it's interpretation – jazb May 01 '19 at 07:10
1

Implement ITransientDependency.

public class EmployeeService : IEmployeeService, ITransientDependency
{
    // ...
}

From https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection#helper-interfaces:

ASP.NET Boilerplate provides the ITransientDependency, the IPerWebRequestDependency and the ISingletonDependency interfaces as a shortcut.

aaron
  • 39,695
  • 6
  • 46
  • 102
1

You can use registered your class in Startup.cs class.here asp..net core provide inbuild DI.

SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32
0

According to the docs

"ASP.NET Boilerplate automatically registers all Repositories, Domain Services, Application Services"

As such all you should need to do is change your IEmployeeService to inherit from IApplicationService:

public interface IEmployeeService : IApplicationService
{
    int CreateEmployee(CreateEmployeeDto data);
    IEnumerable<EmployeeListDto> GetEmployeeList();
}
Jon Ryan
  • 1,497
  • 1
  • 13
  • 29