Search code examples
c#dependency-injectiondryioc

DryIoc : Can't figure out how to register IDisposable Decorator object


I'm trying to setup a decorator around an IDisposable db context:

c.Register<IContext, DbContext>(setup: Setup.With(trackDisposableTransient: true));

// c.Register<IContext, ConsoleTimedContext>(setup: Setup.Decorator); 

If i uncomment the last line, the program throws:

DryIoc.ContainerException: Registered Disposable Transient service Lib.DI.IContext with key {no key} and factory container won't be disposed by container. DryIoc does not hold reference to resolved transients, and therefore does not control their dispose. To silence this exception Register<YourService>(setup: Setup.With(allowDisposableTransient: true)) or set the rule Container(rules => rules.WithoutThrowOnRegisteringDisposableTransient()). To enable tracking use Register<YourService>(setup: Setup.With(trackDisposableTransient: true)) or set the rule Container(rules => rules.WithTrackingDisposableTransient()) à DryIoc.Throw.It(Int32 error, Object arg0, Object arg1, Object arg2, Object arg3) à DryIoc.Container.ThrowIfInvalidRegistration(Factory factory, Type serviceType, Object serviceKey, Boolean isStaticallyChecked) à DryIoc.Container.Register(Factory factory, Type serviceType, Object serviceKey, IfAlreadyRegistered ifAlreadyRegistered, Boolean isStaticallyChecked) à DryIoc.Registrator.Register[TService,TImplementation](IRegistrator registrator, IReuse reuse, Made made, Setup setup, IfAlreadyRegistered ifAlreadyRegistered, Object serviceKey)

How to mix Setup.Decorator with trackDisposableTransient?


Solution

  • The setting is not available per Decorator registration (as of latest DryIoc v2.8.3).

    For now you need to specify non transient reuse for decorator.

    Feel free to raise an issue at: https://bitbucket.org/dadhi/dryioc