You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to consult behaviour of resolved Func dependency on a Singleton object under scope (standard Asp Net DryIoc lifecycle).
If the container is configured to support .WithFuncAndLazyWithoutRegistration(), the Func dependency of even a Singleton object constructed under scope stops working after the scope is disposed, even though no dependency has is marked as Reuse.Scoped.
class Program
{
static void Main(string[] args)
{
using var container = new Container(rules => rules
.WithFuncAndLazyWithoutRegistration());
container.Register<DepFactory>(Reuse.Singleton);
container.Register<Dep>(Reuse.Transient);
DepFactory facoty = null;
using (var scope = container.OpenScope())
{
facoty = scope.Resolve<DepFactory>();
}
var dep = facoty.Create();
}
class DepFactory
{
private readonly Func<Dep> depFunc;
public DepFactory(Func<Dep> depFunc)
{
this.depFunc = depFunc;
}
public Dep Create()
{
// DryIoc.ContainerException: 'code: Error.ContainerIsDisposed;
// message: Container is disposed and should not be used: "container with scope {IsDisposed=true, Name=null}
// with Rules with { FuncAndLazyWithoutRegistration} has been DISPOSED!
return depFunc.Invoke();
}
}
class Dep
{ }
}
Note this is only a problem if the container rules include .WithFuncAndLazyWithoutRegistration(), Without that rule, the code works without exceptions.
Hence from my perspective the current behaviour is
Inconsistent - WithFuncAndLazyWithoutRegistration introducing different (more restrictive) dependency lifecycle behaviour
Undesirable - as despite being resolved under scope (e.g. Asp Net request), none of the dependencies has been registered as Scoped and so the failure is unexpected
I do appreciate that the behaviour is likely just a consequence of the current implementation (invoking disposed scoped resolver), but I think logically this is incorrect and it would make sense to at least offer an option for Func<> dependencies to ignore scope (provided the dependency is not registered as scoped)
The text was updated successfully, but these errors were encountered:
Hi,
I'd like to consult behaviour of resolved Func dependency on a Singleton object under scope (standard Asp Net DryIoc lifecycle).
If the container is configured to support .WithFuncAndLazyWithoutRegistration(), the Func dependency of even a Singleton object constructed under scope stops working after the scope is disposed, even though no dependency has is marked as Reuse.Scoped.
Note this is only a problem if the container rules include .WithFuncAndLazyWithoutRegistration(), Without that rule, the code works without exceptions.
Hence from my perspective the current behaviour is
I do appreciate that the behaviour is likely just a consequence of the current implementation (invoking disposed scoped resolver), but I think logically this is incorrect and it would make sense to at least offer an option for Func<> dependencies to ignore scope (provided the dependency is not registered as scoped)
The text was updated successfully, but these errors were encountered: