Blazor namedscope dependancy injection

68 Views Asked by At

Creating named scoped instances in constructor works in Blazor and WPF. But using IResolutionRoot to create instance only works in WPF. In the sample code WPF works perfectly, the Guid properties confirm that named scope is considered correctly. In blazor ResolutionRoot.Get throws exception which says "The scope ccc is not known in the current context." Does anybody else experience this? Is there maybe a workaround or is this a ninject bug?

I have defined scopes:

            Bind<ICCC>().To<CCC>().DefinesNamedScope("ccc");
            Bind<IBBB>().To<BBB>().InNamedScope("ccc");
            Bind<IAAA>().To<AAA>().InNamedScope("ccc");


            var ccc = Creator.Get<ICCC>();
            ccc.CreateAAA();

classes:

public class AAA : IAAA
{
    public Guid Guid { get; } = Guid.NewGuid();
}

public interface IAAA
{
    Guid Guid { get; }
}

public class BBB : IBBB
{
    public BBB(IAAA aaa)
    {
        Aaa = aaa;
    }

    public Guid Guid { get; } = Guid.NewGuid();
    public IAAA Aaa { get; }
}

public interface IBBB
{
    IAAA Aaa { get; }

    Guid Guid { get; }
}


public class CCC : ICCC
{
    public CCC(IBBB bbb, IAAA aaa, IResolutionRoot resolutionRoot)
    {
        Bbb = bbb;
        Aaa = aaa;
        ResolutionRoot = resolutionRoot;

        Aaa3 = ResolutionRoot.Get<IAAA>();
    }

    public Guid Guid { get; } = Guid.NewGuid();
    public IBBB Bbb { get; }
    public IAAA Aaa { get; }
    public IAAA Aaa2 { get; set; }
    public IAAA Aaa3 { get; set; }
    public IResolutionRoot ResolutionRoot { get; }

    public void CreateAAA()
    {
        Aaa2 = ResolutionRoot.Get<IAAA>();
    }
}

public interface ICCC
{
    IAAA Aaa { get; }

    IAAA Aaa2 { get; }

    IAAA Aaa3 { get; }

    IBBB Bbb { get; }

    Guid Guid { get; }

    void CreateAAA();
}
0

There are 0 best solutions below