Problem with Registration By Convention and Interface Interception

Jul 7, 2014 at 6:18 PM
Edited Jul 7, 2014 at 6:54 PM
I'm having a problem getting interface interceptors to, well, intercept. I'm hoping someone can point me in the right direction.

I'm using Unity Registration by Convention by creating a convention class derived from RegistrationConvention. The setup is I have an IMyService interface, a MyService class that implements the interface, and a MyServiceValidator that serves to validate the class (and which also implements IMyService and ultimately, IInterceptionBehavior).

I know that type mapping is happening properly and that types are being properly registered; just the interceptors aren't seeming to be wired up correctly within the container. I'm just not getting something about how the configuration of all of this is supposed to occur. Here's the portion of the ServiceRegistrationConvention class that's involved:
public class ServiceRegistrationConvention : RegistrationConvention
    public override Func<Type, IEnumerable<InjectionMember>> GetInjectionMembers()
        return GetServiceValidator;

    private IEnumerable<InjectionMember> GetServiceValidator(Type type)
        Type validator = null;

        if (type == null)
            throw new ArgumentNullException("type");

        string matchingValidatorName = string.Concat(type.Name, "Validator");
        // I could cache this query....
        // I need to do this so that libraries can be found for both the MVC web app and
        // the associated integration tests....don't ask :/  Just know, everything that needs to
        // be found is being found at this point.
        validator = AllClasses.FromLoadedAssemblies()
            .Where(t =>
                string.Equals(t.Name, matchingValidatorName, StringComparison.Ordinal)

        return validator != null ?
            new List<InjectionMember> {
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior(validator)
            : new List<InjectionMember>();
Given that snippet of the registration class, I can verify that the validators are being found and matched with their respective class and that, when a validator is found, the list of InjectionMembers is being returned. But obviously I'm configuring something incorrectly because interception is not occurring.

Here's what my container setup looks like:
public static IUnityContainer AutoRegisterServices(this IUnityContainer container)
        .RegisterTypes(new ValidatorRegistrationConvention())
        .RegisterTypes(new ServiceRegistrationConvention())

        // ... Register other stuff

Any help would be appreciated.
Jul 7, 2014 at 7:47 PM
Edited Jul 7, 2014 at 7:48 PM
What I have written here works as advertised. I actually had a problem with another registration, but the error wasn't all that visible; I simply thought my tests were failing. The problem was actually due to a Unity type resolution problem. It turns out that when something was trying to be resolved from the container, an exception was occurring, causing me to think that the registration was not correct and hence why interception was not occurring.
Marked as answer by fourpastmidnight on 7/7/2014 at 12:48 PM
Jul 7, 2014 at 8:56 PM
Thanks for posting an update!

Randy Levy
Enterprise Library support engineer
Support How-to