With Unity 2, if you are trying to replace one registration with another, you will need to specify the type and type From and the type of new registration if they were included in the initial registration.
For example, if you have:
public interface IService { void DoSomething(); } public class SomeService : IService { public void DoSomething(); } public class AnotherService : IService { public void DoSomething(); }
and you will register SomeService as:
container.RegisterType<IService, SomeService>();
then, if another part of your system wants to redefine IService registration for AnotherService, you need to register it as:
container.RegisterType<IService, AnotherService>();
It seems pretty simple, but I hung it when AnotherService needed to be created using factory:
container.RegisterType<IService>(new InjectionFactory(x => { // this would be some complicated procedure return new AnotherService(); }));
In this case, you still get SomeService. To get AnotherService, as you expect, you need to specify a TTo type:
container.RegisterType<IService, AnotherService>(new InjectionFactory(x => { return new AnotherService(); }));
source share