-
Here is a repro of what I have in my app. Is it a bug or I am doing something wrong? https://dotnetfiddle.net/IM8NNC public static void Main()
{
var container = new Container();
container.Register<Wrapper>();
container.Register<Wrapper2>();
var a = new A();
var b = new B();
using (var scope = container.OpenScope(a))
{
container.RegisterDelegate<I>(() => a, Reuse.ScopedTo(a));
Console.WriteLine("Wrapper2 a:" + scope.Resolve<Wrapper2>());
Console.WriteLine("Wrapper a:" + scope.Resolve<Wrapper>());
}
using (var scope = container.OpenScope(b))
{
container.RegisterDelegate<I>(() => b, Reuse.ScopedTo(b));
Console.WriteLine("Wrapper2 b:" + scope.Resolve<Wrapper2>());
Console.WriteLine("Wrapper b:" + scope.Resolve<Wrapper>()); // Fails
}
}
interface I { }
class A : I { }
class B : I { }
class Wrapper2
{
I _i;
public Wrapper2(I i) => _i = i;
public override string ToString() => _i.GetType().FullName;
}
class Wrapper
{
Wrapper2 _w;
public Wrapper(Wrapper2 w) => _w = w;
public override string ToString() => _w.ToString();
} Here the last statement line of the Main fails, however Wrapper2 instance on the previous line is successfully resolved. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@Serg046 Instead of |
Beta Was this translation helpful? Give feedback.
-
In DryIoc scopes are not for separating the registrations but for separating the lifetimes (though workaround exists if you are really know what you are doing). Those are not child containers, but you may inject the runtime known data into them with Use method. |
Beta Was this translation helpful? Give feedback.
@Serg046 Instead of
container.RegisterDelegate<I>(() => a, Reuse.ScopedTo(a));
try thescope.Use<I>(() => a)