The Problem
I'm trying to register a singleton using DryIoc, but container is returning multiple instances of my singleton class. The singleton class is registered as the implementation type for multiple different service interfaces. When any of the aforementioned service interfaces is requested from DryIoc, I expect to get the same instance of my singleton class back, but that's not happening and I don't know why.
An Example
Here's a basic example of what I'm trying to do. In this example, I have a class, Foo
that I would like to use as the singleton implementation for interfaces IFoo
and IBar
. In other words, when either IFoo
or IBar
are resolved from the container, I'd like for the same instance of Foo
to be returned.
The Service Interfaces
interface IFoo
{
}
interface IBar
{
}
The Singleton (Implementation) Class
class Foo : IFoo, IBar
{
}
The Test
Container container = new Container();
container.Register<IFoo, Foo>(Reuse.Singleton);
container.Register<IBar, Foo>(Reuse.Singleton);
object foo = container.Resolve<IFoo>();
object bar = container.Resolve<IBar>();
Assert.AreSame(foo, bar); // Why does this fail?
Considered Solutions
I've considered using DryIoc's RegisterInstance
method but that would require the class to be manually created and I'm trying to avoid that because, unlike the simplified example above, the real-world class has dependencies of its own.