0

我在运行应用程序时遇到此错误消息。

4 System.AggregateException HResult=0x80131500 消息=无法构造某些服务(验证服务描述符“ServiceType:ApplicationName.Application.Interfaces.IUserService Lifetime:Scoped ImplementationType:ApplicationName.Application.Services.UserService”时出错:实施类型'ApplicationName.Application.Services.UserService' 无法转换为服务类型'ApplicationName.Application.Interfaces.IUserService')
Source=Microsoft.Extensions.DependencyInjection StackTrace:在 Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) 在 Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() 在 Microsoft.Extensions.Hosting.HostBuilder.Build() 在 ApplicationName.Client.WebApi.Program.Main(String[] args) 在 C 中: \Development\Projects\eShopping\ApplicationName.Client.WebApi\Program.cs:第 16 行

此异常最初是在此调用堆栈中引发的:[外部代码]

内部异常1:InvalidOperationException:验证服务描述符时出错'ServiceType:ApplicationName.Application.Interfaces.IUserService Lifetime:Scoped ImplementationType:ApplicationName.Application.Services.UserService':实现类型'ApplicationName.Application.Services.UserService'不能转换为服务类型'ApplicationName.Application.Interfaces.IUserService'

内部异常 2:ArgumentException:实现类型“ApplicationName.Application.Services.UserService”无法转换为服务类型“ApplicationName.Application.Interfaces.IUserService”

下面是我正在使用的代码。

// 基础服务接口

public interface IBaseService<T> where T : BaseEntity
{
    IEnumerable<T> GetAll();
    T Get(long id);
    int Insert(T entity);
    int Update(T entity);
    int Delete(T entity);
}



//Generic Implementation Base Service
public class BaseService<T> : IBaseService<T> where T : BaseEntity
{
   
    private readonly IRepository<T> _repository;
   
    public BaseService(IRepository<T> repository)
    {           
        this._repository = repository;
        
    }

    public int Delete(T entity)
    {
        return _repository.Delete(entity);
    }

    public T Get(long id)
    {
        return _repository.Get(id);
    }

    public IEnumerable<T> GetAll()
    {
        return _repository.GetAll();
    }

    public int Insert(T entity)
    {
        return _repository.Insert(entity);
    }

    public int Update(T entity)
    {
        return _repository.Update(entity);
    }
}

//User Servie Inheriting from Base Service
public interface IUserService : IBaseService<User>
{
    public UserViewModel getUserForEmailAddress(string emailAddres);
   
}

//User Service Implemention.
public class UserService : BaseService<User> 
{
    private readonly IMapper _mapper;
    private readonly IUserRepository _userRepository;
    public UserService(IUserRepository userRepository, IMapper mapper) : base(userRepository)
    {
        _userRepository = userRepository;
        _mapper = mapper;
    }

    public int Add(UserViewModel userViewModel)
    {           
        return _userRepository.Insert(_mapper.Map<User>(userViewModel));
    }

    public UserViewModel getUserForEmailAddress(string emailAddres)
    {
         
        return _mapper.Map<UserViewModel>(_userRepository.getUserByEmailAddress(emailAddres));
    }
}


//Startup Register code 

 services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
 services.AddScoped(typeof(IUserRepository), typeof(UserRepository));
 services.AddScoped(typeof(IBaseService<>), typeof(BaseService<>));
 services.AddScoped(typeof(IUserService), typeof(UserService));
4

1 回答 1

0
 public class UserService : BaseService<User> 

应该

 public class UserService : BaseService<User> , IUserService
于 2021-07-18T08:18:52.887 回答