注册新的依赖
Registering new dependency
我正在使用 asp.net boilerplate 创建新项目。
我定义了如下新服务:
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();
}
}
我想使用 HomeController 中的服务:
public class HomeController : MyTestProjectControllerBase
{
IEmployeeAppService service;
public HomeController(IEmployeeAppService svc)
{
service = svc;
}
}
当我 运行 应用程序时,出现以下错误:
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.
如何向 HomeController
注册 EmployeeAppService
依赖项?
更新
我尝试了以下代码
IocManager.Register(typeof(IRepository<Employee, int>),
typeof(EmployeeAppService),
Abp.Dependency.DependencyLifeStyle.Transient);
但随后显示此错误
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.
当您的实体(在本例中为 Employee)未在您的 DbContext 中指定时,通常会发生这种情况。
只需将以下 属性 添加到您的 DbContext class,您就可以开始了:
public virtual IDbSet<Employee> Employees { get; set; }
您无需手动注册应用程序服务。删除代码
IocManager.Register ...
当您基于 IApplicationService 时,它会自动注册到 DI。
错误说 EmployeeAppService 无法解析。所以就像 Jacques Snyman 说的那样,将 Employee 实体添加到 DbContext。
我正在使用 asp.net boilerplate 创建新项目。
我定义了如下新服务:
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();
}
}
我想使用 HomeController 中的服务:
public class HomeController : MyTestProjectControllerBase
{
IEmployeeAppService service;
public HomeController(IEmployeeAppService svc)
{
service = svc;
}
}
当我 运行 应用程序时,出现以下错误:
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.
如何向 HomeController
注册 EmployeeAppService
依赖项?
更新
我尝试了以下代码
IocManager.Register(typeof(IRepository<Employee, int>),
typeof(EmployeeAppService),
Abp.Dependency.DependencyLifeStyle.Transient);
但随后显示此错误
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.
当您的实体(在本例中为 Employee)未在您的 DbContext 中指定时,通常会发生这种情况。
只需将以下 属性 添加到您的 DbContext class,您就可以开始了:
public virtual IDbSet<Employee> Employees { get; set; }
您无需手动注册应用程序服务。删除代码 IocManager.Register ...
当您基于 IApplicationService 时,它会自动注册到 DI。
错误说 EmployeeAppService 无法解析。所以就像 Jacques Snyman 说的那样,将 Employee 实体添加到 DbContext。