I am trying to register a generic type to unitycontainer via configuration file, as shown below in a simplified way.
However on the LoadConfiguration method call I get an error:
"The type name or alias IDataLoader`1[Bar] could not be resolved. Please check your configuration file and verify this type name."
This is the line I get my exception after:
IUnityContainer container = new UnityContainer().LoadConfiguration();
And those are my classes (in the assemblies same name as namespaces):
namespace FooBar.DataManager
{
public interface IDataLoader<TSource>
{
void DoSomeWork(TSource source);
}
}
namespace FooBar.DataManager.MyDataManager
{
public class FooDataLoader : IDataLoader <Bar>
{
public void DoSomeWork(Bar source)
{
Console.WriteLine("Doing {0}", source.Name);
}
}
}
namespace FooBar.DomainModel
{
public class Bar
{
string Name {get; set;}
}
}
And this is the part how I register in the configuration file:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="Bar" type="FooBar.DomainModel.Bar, FooBar.DomainModel" />
<alias alias="IDataLoader`1" type="FooBar.DataManager.IDataLoader, FooBar.DataManager" />
<container>
<register type="IDataLoader`1[Bar]" mapTo="FooBar.DataManager.MyDataManager.DataLoader, FooBar.DataManager.MyDataManager" name="FooBarLoader" />
</container>
</unity>
Can you please advise how should I register the IDataLoader generic type into unitycontainer? Or where I do my mistake? Thanks!
Looking at the documentation again, I think you want to use backticks or square brackets for the open type in the alias target, but not for the closed type or the alias itself:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="Bar" type="FooBar.DomainModel.Bar, FooBar.DomainModel" />
<alias alias="IDataLoader" type="FooBar.DataManager.IDataLoader`1, FooBar.DataManager" />
<container>
<register type="IDataLoader[Bar]" mapTo="FooBar.DataManager.MyDataManager.DataLoader, FooBar.DataManager.MyDataManager" name="FooBarLoader" />
</container>
</unity>
See more on this question at Stackoverflow