简体   繁体   中英

Failed to call service from aspnetboilerplate asp.net-core project controller

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.

You need register it in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IEmployeeService, EmployeeService>();
}

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.

您可以在Startup.cs类中使用已注册的类。此处asp..net核心提供内置DI。

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM