I'm trying to write a JobActivator for HangFire using ServiceStack IoC and I'm having trouble resolving from a type. I'm sure this will be an easy answer for someone with more experience with generics.
The container I'm passing in is coming from HostContext.Container
using Hangfire;
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack;
namespace Common.Hangfire
{
public class FunqJobActivator : JobActivator
{
private Funq.Container _container;
public FunqJobActivator(Funq.Container container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
_container = container;
}
public override object ActivateJob(Type type)
{
return _container.Resolve<type>(); //** this doesn't compile
}
}
}
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:
Which will allow you to resolve registered dependencies using a runtime Type, e.g:
I would recommend using a different IoC framework, because
Funq
does not support a resolve method that takes aType
argument, i.e. it does not have a methodThus the marriage with Hangfire is a difficult one, since Hangfire does not provide an overload that you could use as:
If you insist on using Funq, this can be (inefficiently) solved like this:
I think you want to get a
T
and to return aT