Base solution for your next web application
Open Closed

Dispose a resolved object #8158


User avatar
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)
  • User Avatar
    0
    maliming created
    Support Team

    try

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

    Thanks for your answer.

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

  • User Avatar
    0
    maliming created
    Support Team

    What is the lifestyles of IBlobStorage?

    Singleton or Transient

  • User Avatar
    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)

  • User Avatar
    0
    willignicolas created

    Hello,

    Any idea ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    @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.

  • User Avatar
    0
    willignicolas created

    Hello @ismcagdas,

    Thanks for the answer.

    Great news :)

    Bye.