1
我曾经在一个项目模型下面的类:ASP.NET核心服务登记错误
public interface IUserService
{
IEnumerable<ApplicationUser> GetUsers(string FirstName = null);
ApplicationUser GetUserById(string Id);
ApplicationUser GetUserByUsername(string Username);
void CreateUser(ApplicationUser User);
void SaveUser();
}
public class UserService : IUserService
{
private readonly IUserRepository usersRepository;
private readonly IUnitOfWork unitOfWork;
public UserService(IUserRepository usersRepository, IUnitOfWork unitOfWork)
{
this.usersRepository = usersRepository;
this.unitOfWork = unitOfWork;
}
#region Members
public void CreateUser(ApplicationUser User)
{
throw new NotImplementedException();
}
public ApplicationUser GetUserById(string Id)
{
var user = usersRepository.GetUserById(Id);
return user;
}
public ApplicationUser GetUserByUsername(string Username)
{
var user = usersRepository.GetUserByUsername(Username);
return user;
}
public IEnumerable<ApplicationUser> GetUsers(string FirstName = null)
{
if (string.IsNullOrEmpty(FirstName))
{
return usersRepository.GetAll();
}
return usersRepository.GetAll()
.Where(c => c.Profile.FirstName == FirstName);
}
public void SaveUser()
{
unitOfWork.Commit();
}
#endregion
}
在我的ASP.NET核心(完整的.NET框架),在Startup.cs我有:
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IUserService, UserService>();
我得到一个错误,当我运行应用程序:
InvalidOperationException: Unable to resolve service for type 'Data.Repositories.Identity.IUserRepository' while attempting to activate 'Service.Identity.UserService'.
就像我说的上面,这是一个ASP.NET核心与完整的.NET框架。有什么想法吗?
IUnitOfWork和IUserRepository是否也注册在您的服务集合中?就我所见,这两项在UserService中是必需的。 –
我使用具有以下接口的存储库模式:IDbFactory,IUnitOfWork,IRepository。我没有在我的Startup.cs文件中注册它们中的任何一个。我应该注册所有? –