ArgumentException:实现类型 'UserService' 无法转换为服务类型 'IUserService'
ArgumentException: Implementation type 'UserService' can't be converted to service type 'IUserService'
我在 运行 应用程序时遇到此错误消息。
4 System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the
service descriptor 'ServiceType:
ApplicationName.Application.Interfaces.IUserService Lifetime: Scoped
ImplementationType: ApplicationName.Application.Services.UserService':
Implementation type 'ApplicationName.Application.Services.UserService'
can't be converted to service type
'ApplicationName.Application.Interfaces.IUserService')
Source=Microsoft.Extensions.DependencyInjection StackTrace: at
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.ServiceFactoryAdapter
1.CreateServiceProvider(Object
containerBuilder) at
Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at
Microsoft.Extensions.Hosting.HostBuilder.Build() at
ApplicationName.Client.WebApi.Program.Main(String[] args) in
C:\Development\Projects\eShopping\ApplicationName.Client.WebApi\Program.cs:line
16
这个异常最初是在这个调用栈中抛出的:
[外码]
Inner Exception 1: InvalidOperationException: Error while validating
the service descriptor 'ServiceType:
ApplicationName.Application.Interfaces.IUserService Lifetime: Scoped
ImplementationType: ApplicationName.Application.Services.UserService':
Implementation type 'ApplicationName.Application.Services.UserService'
can't be converted to service type
'ApplicationName.Application.Interfaces.IUserService'
Inner Exception 2: ArgumentException: Implementation type
'ApplicationName.Application.Services.UserService' can't be converted
to service type '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));
public class UserService : BaseService<User>
应该是
public class UserService : BaseService<User> , IUserService
我在 运行 应用程序时遇到此错误消息。
4 System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: ApplicationName.Application.Interfaces.IUserService Lifetime: Scoped ImplementationType: ApplicationName.Application.Services.UserService': Implementation type 'ApplicationName.Application.Services.UserService' can't be converted to service type 'ApplicationName.Application.Interfaces.IUserService')
Source=Microsoft.Extensions.DependencyInjection StackTrace: at 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.ServiceFactoryAdapter
1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at ApplicationName.Client.WebApi.Program.Main(String[] args) in C:\Development\Projects\eShopping\ApplicationName.Client.WebApi\Program.cs:line 16
这个异常最初是在这个调用栈中抛出的: [外码]
Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: ApplicationName.Application.Interfaces.IUserService Lifetime: Scoped ImplementationType: ApplicationName.Application.Services.UserService': Implementation type 'ApplicationName.Application.Services.UserService' can't be converted to service type 'ApplicationName.Application.Interfaces.IUserService'
Inner Exception 2: ArgumentException: Implementation type 'ApplicationName.Application.Services.UserService' can't be converted to service type '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));
public class UserService : BaseService<User>
应该是
public class UserService : BaseService<User> , IUserService