views:

730

answers:

2

Hello, I meet one problem that i can't solve now. I have the following:

UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance);

where UnityHelper.DefaultContainer is my helper for getting unity container with loaded configuration.

here I registered instance as an instance of IMyInterface.

So anywhere(some time after using) I want to remove this mapping. Remove it at all. How I can do it?

I have tried

UnityHelper.DefaultContainer.Teardown(instance)

but is was unsuccessfull and the following code returns instance anyway.

UnityHelper.DefaultContainer.ResolveAll()

Any ideas?
Thank you.

+2  A: 

Hello! I think that is what you are looking for.

var lifetimeManager = new TransientLifetimeManager();
UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance, lifetimeManager);
lifetimeManager.RemoveValue();
er-v
will try this way
bug0r
so, did it work?
er-v
No. it is not. Exception raises when second time resolving run.
bug0r
yes, it should. Something about that interface can not be constructed. That is becouse lifetimeManager.RemoveValue() - removes registration from Unity. The same exception come if you don't register type at all. I thought than this what you wanted. Isn't it?
er-v
Resolution of the dependency failed, type = "IMyInterface", name = "test". Exception message is: The current build operation (build key Build Key[IMyInterface, test]) failed: The current type, IMyInterface, is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3)when resolving
bug0r
exception is raised when ResolveAll is called.
bug0r
All resolves should go before lifetimeManager.RemoveValue() call
er-v
Ok, but what behaviour do you expect for unregistered type?
er-v
You should check registration with IsRegistered method before Resolving.
er-v
do you have my ICQ num?
bug0r
the order was:RegisterInstance(...., lifetimeManager);....ResolveAll() -> exception.....lifetimeManager.RemoveValue();
bug0r
looks like removeValue() and even lifetimemanager.Dispose() does not remove mapping from registeredNames to resolving type.Other words removing values I leave name mapping exist inside container, that cause exception when resolving. Before for TransientLifetimeManager and after for ContainerControlledLifetimeManager.
bug0r
wow, I see.Send me your ICQ [email protected]
er-v
A: 

I had a similar requirement whereby I wanted to temporarily store objects in the unity container and found this was not possible (or at least easily possible). If your objective is to have a temporary storage place easily available to unity, then create a temporary storage service.

public class TemporaryStorageService : ITemporaryStorageService
{
    public void Deposit<T>(Object o, string key)
    {
        System.Windows.Application.Current.Properties[key] = o;
    }

    public T Withdraw<T>(string key)
    {   T o = (T)System.Windows.Application.Current.Properties[key];
        System.Windows.Application.Current.Properties.Remove(key);
        return o;
    }
}

Register your service with Unity. Then when you wish to store an object you call the Deposit Method and when you wish to remove the object you call the Withdraw method. A fuller explanation can be found here

Allan