Grace icon indicating copy to clipboard operation
Grace copied to clipboard

Child container fails to resolve registered depencency

Open publee opened this issue 5 years ago • 3 comments

I am trying to resolve concrete type with dependency registered in child container, but it throws exception.

[Test]
public void TestMethod2()
{
    var container = new DependencyInjectionContainer();
    var childScope = container.CreateChildScope(c => c.Export<Service2>().As<IService2>());

    var service = childScope.Locate<Service>();//LocateException : Could not locate Type TestGraceIoc.UnitTest2+IService2

    Assert.IsInstanceOf<Service>(service);
    Assert.IsInstanceOf<Service2>(service.P1);
}

public interface IService2 { }
public class Service2 : IService2 { }

public class Service 
{
    public Service(IService2 p1)
    {
        P1 = p1;
    }
    public IService2 P1 { get; }
}

publee avatar Oct 17 '20 15:10 publee

@publee I appologize I missed this issue.

Technically speaking by default Grace won't look in a child container for a dependency. You can configure it that way but honestly it makes things slower.

I assume since this is more than a month old you either solved the problem or moved to a different container.

ipjohnson avatar Nov 23 '20 15:11 ipjohnson

Can you please provide example how to configure Grace to look in a child container for a dependency? Thank you.

publee avatar Nov 23 '20 20:11 publee

Hi @publee !

var container = new DependencyInjectionContainer(_ =>
{
  _.Behaviors.ConstructorSelection = ConstructorSelectionMethod.Dynamic;
});

mcdis avatar Jan 21 '21 22:01 mcdis