Auto registering ITypeName

Nov 23, 2011 at 7:58 AM

I am attempting to do simple dependency injection on the MVC3 project.  i built a UnityResolver and created a property on a Home Controller with IMessageService type.  I also have registration configured as follows:

container.ConfigureAutoRegistration()
                .ExcludeSystemAssemblies()
                .ExcludeAssemblies(a => a.GetName().FullName.Contains("Test"))
                .Include(If.ImplementsITypeName, Then.Register().WithTypeName())
                .ApplyAutoRegistration();

 

When  I debug through the DepedencyResolver, I see that attempt to inject HomeController fails due to failure to construct the IMessageService property. 

Strangely, I can construct manually by executing Resolve(typeof(MessageService),"IMessageService").

Can somebody assist?

Coordinator
Nov 23, 2011 at 11:58 AM

Do you really need named registration? Try this:

container.ConfigureAutoRegistration()
                .ExcludeSystemAssemblies()
                .ExcludeAssemblies(a => a.GetName().FullName.Contains("Test"))
                .Include(If.ImplementsITypeName, Then.Register())
                .ApplyAutoRegistration();
Nov 25, 2011 at 10:38 AM

Hi Artem,

Thank you for your suggestion.  However, I am still unable to inject via property injection.  I built a quick debug action to print out registrations and see that IMessageService is still registered with MessageService implementation but as named registration. The Mvc 3 depedency resolver does not work well with named registrations.

On a side note, I also noticed that default scan settings would register each implementation of IMessageService  i.e.  MessageService1, MessageService2 with the named registrations as well.  I'd presume this is by design.  My issue here is that i would only want to pick up ITypeName and TypeName if the names match and interface just have a "I" prefix convention.

Coordinator
Nov 28, 2011 at 9:33 PM

Hello,

Could you please attach minimal solution reproducing the problem, I'll have a look.

Thanks.