Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering Collections in Autofac 2.1.10 RC

Tags:

autofac

I am upgrading code from Autofac 1.4 to 2.1.10 Release Candidate.

My module previously performed registration like this:

builder.RegisterCollection<IExceptionHandler>()
                .As<IEnumerable<IExceptionHandler>>()
                .FactoryScoped();
builder.Register<AspNetExceptionHandler>()
                .As<IExceptionHandler>()
                .MemberOf<IEnumerable<IExceptionHandler>>()
                .FactoryScoped();

Now, RegisterCollection has no parameterless overload. I don't care about assigning it a name. Assuming it's OK to just pass in null, my code looks like this in 2.1:

builder.RegisterCollection<IExceptionHandler>(null)
                .As<IEnumerable<IExceptionHandler>>()
                .InstancePerDependency();
builder.RegisterType<AspNetExceptionHandler>()
                .As<IExceptionHandler>()
                .MemberOf<IEnumerable<IExceptionHandler>>(null)
                .InstancePerDependency();

However, when I compile, I get the following error regarding .MemberOf:

Using the generic method 'Autofac.RegistrationExtensions.MemberOf(Autofac.Builder.RegistrationBuilder, string)' requires '3' type arguments

I tried putting in a collection name instead of null, just in case, and that had no effect.

What's the proper way to register collections in 2.1?

like image 848
moribvndvs Avatar asked Feb 24 '10 21:02

moribvndvs


1 Answers

As I understand it, you just register a bunch of IExceptionHandler types, and then when you request an IEnumerable<IExceptionHandler> Autofac 2 will just take care of everything for you.

From the NewInV2 page:

builder.RegisterType<A1>().As<IA>();
builder.RegisterType<A2>().As<IA>();

var container = builder.Build();

// Contains an instance of both A1 and A2
Assert.AreEqual(2, container.Resolve<IEnumerable<IA>>().Count());
like image 187
Joel Mueller Avatar answered Oct 16 '22 02:10

Joel Mueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!