Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

ninject - Blazor namedscope dependancy injection

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();
}
question from:https://stackoverflow.com/questions/65916599/blazor-namedscope-dependancy-injection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...