How to resolve type using ServiceStack Funq IoC

╄→гoц情女王★ 提交于 2019-12-02 02:31:44

Whilst Funq is a typed IOC with Generic API's, you can add a helper extension method to enable resolving instances using a runtime type, e.g:

public static class ContainerExtensions
{
    public static object TryResolve(this Container container, Type type)
    {
        var mi = typeof(Container).GetMethods(BindingFlags.Public | BindingFlags.Instance)
            .First(x => x.Name == "TryResolve" &&
                   x.GetGenericArguments().Length == 1 &&
                   x.GetParameters().Length == 0);

        var genericMi = mi.MakeGenericMethod(type);
        var instance = genericMi.Invoke(container, new object[0]);
        return instance;
    }
}

Which will allow you to resolve registered dependencies using a runtime Type, e.g:

var container = new Container();
container.Register(c => new Foo());

var instance = container.TryResolve(typeof(Foo));

I would recommend using a different IoC framework, because Funq does not support a resolve method that takes a Type argument, i.e. it does not have a method

object Container.Resolve(Type theType);

Thus the marriage with Hangfire is a difficult one, since Hangfire does not provide an overload that you could use as:

virtual T ActivateJob<T>() where T : class

If you insist on using Funq, this can be (inefficiently) solved like this:

public class FunqJobActivator : JobActivator
{
    private const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
    private static readonly _activateMethod = 
        typeof(FunqJobActivator).GetMethod("InternalActivateJob", flags);

    private Funq.Container _container;

    public FunqJobActivator(Funq.Container container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        _container = container;
    }

    public override object ActivateJob(Type type)
    {
        // this will allow calling InternalActivateJob<T> with typeof(T) == type.
        var method = _activateMethod.MakeGenericMethod(new [] { type });
        return method.Invoke(this, null);
    }

    private object InternalActivateJob<T>() where T : class
    {
        return _container.Resolve<T>();
    }
}

I think you want to get a T and to return a T

public T ActivateJob<T>() where T : class
{
    return _container.Resolve<T>();  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!