Hi this seems wrong to me. Is this the way it was designed?
My disposable class:
class C : IDisposable
{
public void Dispose()
{
Console.WriteLine("Disposing C");
}
}
Registration:
cb.RegisterInstance(new C());
Usage:
using (IContainer container = BuildContainer())
{
var c = container.Resolve<C>();
Console.WriteLine("C resolved");
}
Output:
C resolved
Disposing C
Disposing C
I think its a bad thing to call Dispose multiple times on the same object.
Note:
When I register the class like this
cb.Register(c => new C());
It gets disposed only once. Why the difference?
It isn’t,
Disposeis supposed to be safe to call multiple times. From the documentation: “The object must not throw an exception if its Dispose method is called multiple times.” Because this is supposed to be safe, you shouldn’t rely on other libraries only calling it once, and there’s nothing wrong with changes that you feel shouldn’t make a difference causing multipleDisposecalls.