0

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!

3
  • 1
    Your alias appears to try to alias a non-generic IDataLoader type. Have you tried <alias alias="IDataLoader'1" type="FooBar.DataManager.IDataLoader'1, FooBar.DataManager" /> ? (But with backticks instead of apostrophes... I can never get them to work in comments here...) Commented May 22, 2015 at 5:48
  • Hi Jon, yes I tried that.I am suspicious with the syntax whether I use it correctly or not to be honest. But I cannot see mistake. Commented May 22, 2015 at 5:56
  • Looking at msdn.microsoft.com/en-us/library/dn507491(v=pandp.30).aspx, I don't think you should have the backticks anywhere, in fact. It's not clear how aliases work with generics though... (As an aside, I haven't used Unity myself - if you're able to put together a short but complete example that we could easily use to reproduce the problem, that'd be great.) Commented May 22, 2015 at 5:58

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Jon, did you put the backtick end of the assembly name by purpose? (FooBar.DataManager`1) Does not that remarks the generic type and the number of parameters that is bound to type, but not the assembly?
@pencilCake: Nope, that was a stupid typo - fixed now, thanks. Whether it helps will be a different matter...
@jlvaquero: Sorry, I don't quite understand your comment. Your other answer suggests that alias is just a string replacement - I don't believe it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.