Skip to content
This repository has been 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 opened this issue Apr 13, 2017 · 3 comments
Closed

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

jaredcnance opened this issue Apr 13, 2017 · 3 comments
Assignees
Milestone

Comments

@jaredcnance
Copy link

jaredcnance commented Apr 13, 2017

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.

@muratg muratg added this to the 2.0.0 milestone Apr 14, 2017
@muratg
Copy link

muratg commented Apr 14, 2017

@pakrym Let's investigate for 2.0.0.

@pakrym
Copy link
Contributor

pakrym commented Jun 1, 2017

I'm not able to reproduce your issue, can you please provider a repro code that fails?

@pakrym
Copy link
Contributor

pakrym commented Jun 21, 2017

Closing, please reopen if you still see this issue.

@pakrym pakrym closed this as completed Jun 21, 2017
@pakrym pakrym added 3 - Done and removed 1 - Ready labels Jun 21, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants