2

I have some code that registers types with strongly typed metadata. It looks something like this:

class Foo { }

public interface IFooMetadata
{
    int Position { get; }
}

[TestFixture]
public class MyTestFixture
{
    [Test]
    public void Test()
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<Foo>()
            .AsSelf()
            .WithMetadata<IFooMetadata>(m => m.For(x => x.Position, 1));

        using (var container = builder.Build())
        {
            var fooWithMeta = container.Resolve<Meta<Foo, IFooMetadata>>();
        }
    }
}

I've just updated my code to use the current version of Autofac (3.0.2) and it seems that any types that are registered this way cannot be resolved (ComponentNotRegisteredException).

I wrote the above test and it passes with Autofac 2.6.1.841, but throws a ComponentNotRegisteredException in 3.0.2.

Am I missing something? Is Meta<T, TMetadata> still the way to go, or is there a new way to do this?

Matt Siebert
  • 367
  • 1
  • 9

1 Answers1

2

There are a lots of breaking changes in Autofac 3.0.

So the Interface Based Metadata support was moved out from the Autofac core to the MEF integration package.

So you need to get the Autofac.Mef package and call the RegisterMetadataRegistrationSources() extension method on the builder as described in the documentation.

var builder = new ContainerBuilder();

builder.RegisterMetadataRegistrationSources()

builder.RegisterType<Foo>()           
       .AsSelf()
       .WithMetadata<IFooMetadata>(m => m.For(x => x.Position, 1));

You can read about more this breaking change in this article: Autofac 3.0 Beta packages available on NuGet

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • After re-reading the documentation I see that I can use metadata classes instead of interfaces, and this avoids the extra dependency on the Autofac.Mef package. This seems like a better solution for my scenario. – Matt Siebert Jun 06 '13 at 23:12