Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

"Could not resolve service for type" on unreachable path #514

Closed
@jaredcnance

Description

@jaredcnance

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions