I have inherited a code base that makes use of the Castle Windsor IoC container, we have recently been forced to upgrade version v2.5.2 from 3.0.0 from due to another compatibility issue.
Following the upgrade to v3.0.0, the following extension method in one of our test classes fails to build with the following error:
The type ‘TInterface’ must be a reference type in order to use it as parameter ‘TService’ in the generic type or method ‘Castle.MicroKernel.Registration.ComponentRegistration’
public static class ContainerExtensions
{
/// <summary>
/// Sets the registration expectation on the mocked container.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
public static void SetRegistrationExpectation<TInterface, TImplementation>(this IWindsorContainer container) where TImplementation : TInterface
{
Predicate<IEnumerable<IRegistration>> pred = regs =>
{
var reg = regs.OfType<ComponentRegistration<TInterface>>().FirstOrDefault();
return reg != null && reg.Implementation == typeof(TImplementation);
};
container
.Expect(c => c.Register(null))
.IgnoreArguments()
.Constraints(Rhino.Mocks.Constraints.Is.Matching(pred))
.Repeat.Once();
}
}
So it would seem ComponentRegistration can no longer take an interface? Having looked at the documentation I am still unsure how to rectify?
Any pointers would be appreciated.
Try to add
where TInterface : classconstraint to your method:It seems like
ComponentRegistrationgeneric constraints changed in 3.0.