Amol Gawai
Amol Gawai

Reputation: 3350

Unity Interception: Interface interception for an interface with events

I am trying to use unity interception to intercept exceptions. I defined an interface and a class that implements that Interface.

I am trying to use InterfaceInterception, but Unity is throwing an exception that the type is not interceptable. I have checked my class and the only difference from other classes that are successfully intercepted is that this class and the interface declare some event. If I use VirtualMethodInterception, Unity does not throw an exception.

I am using c#

Why is VirtualMethodInterceptor working and why is the InterfaceInterceptor not working?

Following is a simplification of actual code.

public interface IInterfaceToBeInterceptable : IDisposable
{
    event EventHandler<ItemSavedEventArgs> ItemSaved;

    event EventHandler<GenericEventArgs<bool>> JobWasCompleted;

    InvocationResult InvokeJob( JobInvocationParams invocationParams );
}

public class ClassToBeInterceptable : IInterfaceToBeInterceptable
{
    public event EventHandler<ItemSavedEventArgs> ItemSaved;

    public event EventHandler<GenericEventArgs<bool>> JobWasCompleted;

    public InvocationResult InvokeJob( JobInvocationParams invocationParams )
    {
        // Implementation
    }

    public void Dispose()
    {
        // Implementation
    }

    private static GetInfo()

    {
        // Implementation
    }
}

and the unity configuration is

<register type="IInterfaceToBeInterceptable" mapTo="ClassToBeInterceptable">
     <lifetime type="singleton" />

     <interceptor type="InterfaceInterceptor" />
     <interceptionBehavior type="Exceptionbehavior"  />
     <interceptionBehavior type="LoggingBehaviour" />
  </register>

The exception details are as follows

Exception is: ArgumentException - The type ClassToBeInterceptable is not interceptable. Parameter name: interceptedType

EDIT :-

This class is specified as dependency for another class and the above exception is thrown during build up of another class which has dependency on the "another" class ( or to be precise, interface of the "another" class).

Upvotes: 1

Views: 1488

Answers (0)

Related Questions