Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created August 20, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/a631b002ce761ac5d64d77ab38c705e8 to your computer and use it in GitHub Desktop.
Save dcomartin/a631b002ce761ac5d64d77ab38c705e8 to your computer and use it in GitHub Desktop.
using Unity;
using Unity.Lifetime;
namespace UnityFactory
{
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterFactory<IFoo>(x =>
{
// This fails because x is not the container/child that is trying to resolve of IFoo, but rather the parent container that did the RegisterFactory
return new Foo(x.Resolve<IBar>());
}, new ContainerControlledLifetimeManager());
var child = (container as IUnityContainer).CreateChildContainer();
child.RegisterType<IBar, Bar>(new ContainerControlledLifetimeManager());
// This fails
var bar = child.Resolve<IFoo>();
}
}
public interface IFoo { }
public class Foo : IFoo
{
public Foo(IBar bar)
{
}
}
public interface IBar {}
public class Bar : IBar {}
}
@ENikS
Copy link

ENikS commented Aug 21, 2020

Search issues in DI repo, you'll find reason for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment