0

I am trying to use following example provided in Microsoft site. gives compile time error while resolving by type.

http://msdn.microsoft.com/en-us/library/vstudio/hh323725(v=vs.100).aspx

is it issue in the code, how to solve this?

 public class DependencyInjectionInstanceProvider : IInstanceProvider
{
    private readonly Type _ServiceType;

    public DependencyInjectionInstanceProvider(Type serviceType)
    {
        _ServiceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return DependencyFactory.Resolve(_ServiceType);  //error
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
    }
}


 public class DependencyFactory
{
    private static IUnityContainer _container;

    public static IUnityContainer Container
    {
        get
        {
            return _container;
        }
        private set
        {
            _container = value;
        }
    }

    static DependencyFactory()
    {
        var container = new UnityContainer();

        var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        if (section != null)
        {
            section.Configure(container);
        }
        _container = container;
    }

    public static T Resolve<T>()
    {
        T ret = default(T);

        if (Container.IsRegistered(typeof(T)))
        {
            ret = Container.Resolve<T>();
        }

        return ret;
    }

}
anand
  • 307
  • 3
  • 14

2 Answers2

0

Sorry for the long time answer, but I started Unity only in these days. Maybe you have to call in this way:

  DependencyFactory.Resolve<_ServiceType>();  

I think the problem is you are treating a Type like a parameter in the caller. Take a look at this link http://msdn.microsoft.com/en-us/library/ff650319.aspx

zappasan
  • 82
  • 10
0

The sample code has a DependencyFactory.Resolve<T>() but not a DependencyFactory.Resolve(Type typeToResolve).

You could implement DependencyFactory.Resolve(Type typeToResolve) duplicating the example logic (refer to Programmatic equivalent of default(Type) to create a default value using a Type).

The most simple resolution is to access the container member.

Make sure to including using Microsoft.Practices.Unity;

public object GetInstance(InstanceContext instanceContext, Message message)
{
    return DependencyFactory.Container.Resolve(_serviceType);
}
Community
  • 1
  • 1
Sean M
  • 616
  • 6
  • 16