As the title says we’re in the process of migrating a system from .NET Framework to .NET Core. The original system used Ninject for DI migrating away from this has given us some trouble. There’s quite a bit of generated code in this project which uses generics.
The error that has us stuck is a runtime error:
Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: ProjectNamespace.Core.Services.IAddressService Lifetime: Scoped ImplementationType: ProjectNamespace.Core.Services.AddressService’: Unable to resolve service for type ‘ProjectNamespace.Core.AddressMapper’ while attempting to activate ‘ProjectNamespace.Core.Repositories.AddressRepository’.) (Error while validating the service descriptor ‘ServiceType: ProjectNamespace.Core.Repositories.IAddressRepository Lifetime: Scoped ImplementationType: ProjectNamespace.Core.Repositories.AddressRepository’: Unable to resolve service for type ‘ProjectNamespace.Core.AddressMapper’ while attempting to activate ‘ProjectNamespace.Core.Repositories.AddressRepository’.)
Code:
// Startup.cs
services.AddGeneratedComponents();
// GeneratedRegistrations.cs
public static IServiceCollection AddGeneratedComponents(this IServiceCollection services)
{
AddMySqlComponents(services);
return services;
}
public static IServiceCollection AddMySqlComponents(this IServiceCollection services)
{
GenericsHelper.Initialize(services);
services.AddScoped(typeof(IGenericRepository<,,>), typeof(GenericRepository<,,>));
return services;
}
// Generics.cs
public static IServiceCollection Initialize(this IServiceCollection services)
{
services.AddScoped<IAddressMapper, AddressMapper>();
services.AddScoped<IAddressService, AddressService>();
services.AddScoped<IAddressRepository, AddressRepository>();
return services;
}
public partial interface IAddressRepository : IGenericRepository<Address, AddressSearchCriteria, AddressMapper>
{
}
public partial class AddressRepository : GenericRepository<Address, AddressSearchCriteria, AddressMapper>, IAddressRepository
{
public AddressRepository(AddressMapper mapper)
: base(mapper)
{
}
}
Any help would be greatly appreciated, been wrestling with this for a few days now.
Not sure exactly where to start with this error. Happens a runtime and the source isn’t clear to me. It’s all happening under the hood so to speak.
JohnnyCodes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2