17

How can I make Unity not to throw ResolutionFailedException if Resolve fails?

Is there something like TryResolve<IMyInterface>?

var container = new UnityContainer();
var foo = container.TryResolve<IFoo>();
Assert.IsNull(foo);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Vadim
  • 21,044
  • 18
  • 65
  • 101

6 Answers6

16

Also note that, if you're using Unity 2.0 you can use the new IsRegistered() method and it's generic version as well.

Bryan Ray
  • 931
  • 10
  • 26
  • 10
    One [big warning](https://unity.codeplex.com/discussions/392550) about the IsRegistered method: It is only meant for debugging because it is notoriously slow! It has a performance characteristic of O(n) and can completely drown the performance of your application. – Steven Aug 22 '13 at 06:45
9

This has been an issue on the codeplex site, you can find the code here (look at the bottom of that thread and they have made an extension method...very handy)

http://unity.codeplex.com/Thread/View.aspx?ThreadId=24543

and the you can use code like this:

if (container.CanResolve<T>() == true)
{
    try
    {
        return container.Resolve<T>();
    }
    catch (Exception e)
    {
        // do something else
    }
}

CanResolve is that extension method. I'm actually registering that extension upon creation of the container...something like this:

private void CreateContainer()
{
    ExeConfigurationFileMap map = new ExeConfigurationFileMap();

    map.ExeConfigFilename = // path to config file

    // get section from config code goes here

    IUnityContainer container = new UnityContainer();
    container.AddNewExtension<UnityExtensionWithTypeTracking>();
    section.Containers.Default.Configure(container);        
}
Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
Johan Leino
  • 3,473
  • 1
  • 26
  • 27
3

It seems that it lacks this feature. This article shows the example of enclosing Resolve method in the try/catch block to implement it.

public object TryResolve(Type type)
{
    object resolved;

    try
    {
        resolved = Resolve(type);
    }
    catch
    {
        resolved = null;
    }

    return resolved;
}
bbmud
  • 2,678
  • 21
  • 15
  • Thanks for the great answer. I wasn't sure if my question was clear but your answer is exactly what I was looking for. – Vadim May 18 '09 at 18:20
2

This is not available in the current release. However, you can always "roll your own" using extension methods in C# 3. Once Unity supports this, you can omit or update the extension method.

public static class UnityExtensions
{
    public static T TryResolve<T>( this UnityContainer container )
        where T : class
    {
        try
        {
            return (T)container.Resolve( typeof( T ) );
        }
        catch( Exception )
        {
            return null;
        }
    }
}
LBushkin
  • 129,300
  • 32
  • 216
  • 265
0

In Prism Unity 5, they have come up with the TryResolve function that has been included in the namespace Microsoft.Practices.Prism.UnityExtensions.

Please go through this link https://msdn.microsoft.com/en-us/library/gg419013(v=pandp.50).aspx for reference.

karthik
  • 17,453
  • 70
  • 78
  • 122
0
IComponent component= null;

if (c.IsRegistered<IComponent>(registrationName))
{
  component= c.Resolve<IComponent>(registrationName);
}

return component;
Amir Touitou
  • 3,141
  • 1
  • 35
  • 31