I want to resolve a specific interface implementation from autofac based on the type of an input parameter passed at runtime.
In my code I have different implementations of IMyTask<>
:
MySillyTask : IMyTask<SillyData>
MyAwesomeTask : IMyTask<AwesomeData>
I register the implementations of MyTask<>
like this:
builder.RegisterAssemblyTypes(typeof (IMyTask<>).Assembly)
.AsClosedTypesOf(typeof (IMyTask<>));
I want use a class with an injected Autofac IComponentContext
to resolve the data based on the type of Data passed:
public IMyTask<T> GetTask<T>(T input) where T : IData
{
var myTask = _componentContext.Resolve<IMyTask<T>>(); // fails
return myTask;
}
I get The requested service has not been registered
error.
If I try to resolve it for a hardcoded IData
type it works:
var myTask = _componentContext.Resolve<IMyTask<SillyData>>(); // works! MySillyTask resolved
I think it is because at runtime T
is IData
instead of the specific IData
implementation I pass but is there a way to change the code to get the functionality I require?
EDIT: As requested the complete error message:
The requested service 'Whatever.Tasks.IMyTask`1[[Whatever.Inputs.IData, Whatever, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
How I am calling GetTask
:
IData myData = input.MyData;
taskResolver.GetTask(myData);
I think I realise it is because T
is IData
instead of an implementation that causes my code to fail. Though I have changed my code to use named instances and returning a non-generic 'IMyTask' I am still interested if I can use autofac to resolve it the way I originally intended.