AustinOriginal
AustinOriginal

Reputation: 13

Scanning, and overriding autofac registrations that use WithParameter

As part of my autofac initialization, I scan a list of assemblies and register any services that match a naming convention: MyClass: IMyClass. This cuts down on the amount of registration code I have so I really don't want to eliminate this feature. The problem is that I have a couple classes that I want to register using some specific initialization data. I'm currently registering these after the scanning process using something like:

builder.RegisterType<MyClass>().As<IMyClass>().WithParameter("initData", value)

When I examine the container I can see both registrations, the one from the scanning process and the one with the specified parameter. Is this the correct behavior? I was wanting autofac to replace the scanned registration for IMyClass. How can I get autofac to use the registration using the provided parameter?

Thanks,

Upvotes: 1

Views: 246

Answers (1)

Peter Lillevold
Peter Lillevold

Reputation: 33910

The container will keep both registrations, but only the last registration will be used. Have you made a test to verify the behavior you expect? Something along these lines:

var my = container.Resolve<IMyClass>();
Assert.That(my.Data, Is.EqualTo("initData"));

Upvotes: 1

Related Questions