Open Closed

Dispose a resolved object #8158


0
willignicolas created

Hello,

I have injected Two instances from the same interface like that :

 IBlobStorage storage = StorageFactory.Blobs.DirectoryFiles(directory);
            _iocManager.IocContainer.Register(
                Castle.MicroKernel.Registration.Component.For<IBlobStorage>().Instance(storage).Named(fileProviderName)
            );

I resolve this instance like that :

 FileProvider = _iocManager.IocContainer.Resolve<IBlobStorage>(FileProviderName);

I don't use IIocResolver or IScopedIocResolver because they don't have a resolve method by name but all work fine with that method.

I have read that if i resolve my instances with iocContainer I must release it. I'm in a domainservice context but I don't know where to do the release (I use my instance in many methods in my domain service). I have put a IDisposable interface on my domainservice but the debugger never go to the dispose method.

Have you an idea where to put this release or maybe on other method to resolve the named instance?

Thanks for your help.

Nicolas.


7 Answer(s)
  • 0
    maliming created

    try

    using (var scope = _iocResolver.CreateScope())
    {
        var FileProvider = _iocManager.IocContainer.Resolve<IBlobStorage>(FileProviderName);
        //...
    }
    
  • 0
    willignicolas created

    Thanks for your answer.

    I have 9 methods that use FileProvider in my domainservice so I must do that every time ?

  • 0
    maliming created

    What is the lifestyles of IBlobStorage?

    Singleton or Transient

  • 0
    willignicolas created

    Maliming,

    IBlobStorage come from the storage.net nuget. An instance is provide by a factory method provided by the nuget and the instance is injected in castle windsor with a name. So it's a singleton.

    There is the registration code called twice (for 2 instances of IBlobStorage) in the postInitialize method of my core module :

     IBlobStorage storage = StorageFactory.Blobs.DirectoryFiles(directory);
                _iocManager.IocContainer.Register(
                    Castle.MicroKernel.Registration.Component.For<IBlobStorage>().Instance(storage).Named(fileProviderName)
                );
    

    Hope this will help you to find the best implementation.

    (It's pity that the register method of IScopedIocResolver do not take a string parameter to resolved the named injected instances)

  • 0
    willignicolas created

    Hello,

    Any idea ?

  • 0
    ismcagdas created

    @willignicolas, if it is singleton, then you don't need to resolve it.

    I have read that if i resolve my instances with iocContainer I must release it.

    This is mostly valid for transient dependencies.

  • 0
    willignicolas created

    Hello @ismcagdas,

    Thanks for the answer.

    Great news :)

    Bye.