Microsoft Unity - Configuration and Aliases

Author: Miguel Posted 30. November 2011

Should I Use Aliases in my Configuration File?

It's a good idea to use aliases for the following two reasons:

  1. User Friendly
    1. It allows you to keep all your assembly references at the top and easily viewable, so you don't have to sift through all the registration tags to search for the references you need to add to the project, and
  2. Better Error Display
    1. You will get more useful errors should you reference the incorrect assembly or if you don't reference the assembly you are specifying in the configuration.

If you are using the registration tags to specify the full type names and assemblies, and one of them happens to be misspelt or you haven't referenced that project in your assembly, Unity will throw a very obscure message that will look like this: "FileLoadException was unhandled by user code. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"

This particular error sent me on a wild goose chase for a good few hours until I decided to use the alias tags. After which it broke immediately and indicated exactly the type that was giving the issues. It was coincidence in fact, since I wanted to clean up my code so that I could easily see all the references that I was using at the top as opposed to sifting through my reference tags. It was after I added in the references that the error it gave me was actually quite descriptive and not obscure like the previous one. In my case, it was a silly misspelling of the reference which would've taking me ages to figure out had I have persisted based on the current error message. In fact, in one post I read the guy actually downloaded the source code for unity and debugged it this way, obviously causing a huge delay in his project.

So it makes sense then to use aliases, as it cleans up your config file quite a bit and due to Unity appearing to resolve the aliases first, will give you a better error message should the type you referenced be unavailable/misspelt etc.

Hope this saves you guys some time. Let me know your thoughts.


Global ASP.NET Hosting Leader - Click Here

Comments (1) -

Daniel Milstein
Daniel Milstein Iceland
1/18/2012 4:41:22 AM #

That is so true Configuration and Aliases  As an author and business man, I like how you said "This particular error sent me on a wild goose chase for a good few hours until I decided to use the alias tags".  I hope more people discover your blog because you really know what you're talking about.  Can't wait to read more from you!


Add comment

  Country flag

  • Comment
  • Preview

Global ASP.NET Hosting Leader - Click Here