"Could not resolve service for type" on unreachable path #514
Description
dotnet --version: 1.0.0
framework: netcoreapp1.0
Suppose an app uses the following dependency chain:
Controller
|--Service<T>
|----Repository<T>
public class MyModelsController
{
public MyModelsController(IResourceService<MyModel> resourceService)
{ }
// ...
}
And a library may be used to define some "default" services like so:
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
public class EntityResourceService<T> : IResourceService<T> where T : class
{
public EntityResourceService(IEntityRepository<T> entityRepository)
{ }
// ...
}
Now suppose the application injects a specific service that implements IResourceService<MyModel>
.
services.AddDefaults(); // assume this is just the code above
services.AddScoped<IResourceService<MyModel>, MyModelService>();
public class MyModelService : IResourceService<MyModel>
{
MyModelService()
{ }
// ...
}
DI will resolve this more specific dependency instead of the unbound generic. However, it still checks the unbound generic dependency chain (dependencies of EntityResourceService<>
). If MyModelService
has no dependency on IEntityRepository<>
, but DefaultEntityRepository<>
has dependencies that are not in the container it will fail with
Unable to resolve service for type ... while attempting to activate ...DefaultEntityRepository
If the dependencies are added, it works, but resolves the MyModelService
not the EntityResourceService<>
. Hopefully this makes sense.