Unity.AutoRegistration not auto-registering

1.5k Views Asked by At

I'm trying to use Unity.AutoRegistration to auto wire my interfaces to the implementations. My configuration looks like this:

public static class UnityConfigurator
{
    public static UnityContainer Configure()
    {
        var container = new UnityContainer();
        container.ConfigureAutoRegistration()     
                 .LoadAssemblyFrom(typeof(UnityConfigurator).Assembly.Location)
                 .LoadAssemblyFrom(typeof(ICountryFilterDataRepository).Assembly.Location)
                 .ExcludeSystemAssemblies()
                 .ExcludeAssemblies(a => a.GetName().FullName.Contains("Specs"))                     
                 .ApplyAutoRegistration();
        return container;
    }
}

But it's not working :( I get this error, which clearly implies the mapping hasn't been set:

The current type, Blah.IFoo, is an interface and cannot be constructed. Are you missing a type mapping?

What's wrong with my config code? Thanks

2

There are 2 best solutions below

0
On

Maybe you want to try an alternative to Unity.AutoRegistration.

The TecX project on CodePlex has a port of the StructureMap configuration engine including its support for registration by convention. The configuration for Unity can be found in TecX.Unity.Configuration. There are UnitTests that show how it is used.


UPDATE:

With TecX auto registration of IFoo would look like this:

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.Scan(
  x =>
  {
    x.AssembliesFromApplicationBaseDirectory();
    x.With(new ImplementsIInterfaceNameConvention());
  });
0
On

Adding this line should fix the issue

.Include(If.ImplementsITypeName, Then.Register())