1

I am trying to use Interception Call Handler and Handler Attributes on my interfaces (or implementation). Lets say my Interface has two methods DoSomething() and DoSomethingElse(), and I only have the interceptor for DoSomethingElse(); when I resolve my main interface the constructor for the Call Handler get called even if I never invoke DoSomethingElse().

I tried this by resolving to Lazy(of IMainInterface) but the moment I call the function DoSomething() the Call Handler is still created unnecessarily. Is there a way to prevent this from happening either by code or configuration. Here is my sample implementation

Handler Attribute and Call Handler

<AttributeUsage(AttributeTargets.Method)>
Public Class NormalSampleAttribute
    Inherits HandlerAttribute
    Public Sub New()
            Console.WriteLine("Constructor of Normal Sample Attribute")
    End Sub

    Public Overrides Function CreateHandler(container As Microsoft.Practices.Unity.IUnityContainer) As Microsoft.Practices.Unity.InterceptionExtension.ICallHandler
            Console.WriteLine("Create Handler")
            Return New SampleCallHandler
    End Function
    End Class

    Public Class SampleCallHandler
    Implements ICallHandler
    Public Sub New()
            Console.WriteLine("Constructor of Sample Call handler")
    End Sub

    Public Function Invoke(input As IMethodInvocation, getNext As GetNextHandlerDelegate) As IMethodReturn Implements ICallHandler.Invoke
            Console.WriteLine("Invoke  of Sample Call handler - " & input.MethodBase.Name)
            Return getNext.Invoke(input, getNext)
    End Function
    Public Property Order As Integer Implements ICallHandler.Order
End Class

Interface and implementation

Public Interface IMainInterface
    Sub DoSomething()
    <NormalSample()>
    Sub DoSomethingElse()
End Interface

Public Class MainClass
    Implements IMainInterface

    Public Sub New()
            Console.WriteLine("main class - Constructor")
    End Sub

    Public Sub DoSomething() Implements IMainInterface.DoSomething
            Console.WriteLine("main class do something...")
    End Sub

    Public Sub DoSomethingElse() Implements IMainInterface.DoSomethingElse
            Console.WriteLine("main class do something else...")
    End Sub
End Class

Main module to register and execute the method

Module Module1
    Public container As IUnityContainer
    Sub Main()
            container = New UnityContainer
            DoRegistrations()
            Console.WriteLine("Before lazy  Resolve")
            Dim lmc As Lazy(Of IMainInterface) = container.Resolve(Of Lazy(Of IMainInterface))()
            Console.WriteLine("Before making lazy function call")
            lmc.Value.DoSomething()
            Console.ReadLine()
    End Sub

    Sub DoRegistrations()
            container.AddNewExtension(Of InterceptionExtension.Interception)()
            container.RegisterType(Of IMainInterface, MainClass)()
            container.Configure(Of Interception).SetDefaultInterceptorFor(Of IMainInterface)(New InterfaceInterceptor)
    End Sub
End Module

It produces the following output:

Before lazy Resolve
Before making lazy function call
main class - Constructor
Constructor of Normal Sample Attribute
Create Handler
Constructor of Sample Call handler

main class do something...

Even though DoSomethingElse() is never called the cost of the handler creation is being added on all flows. Is there any way to avoid this? Any help is appreciated.

Thanks in advance! SV

  • Can you make a shorter (Testable) code? – chouaib Oct 08 '14 at 00:50
  • 1
    @chouaib I am not sure I understand your request for shorter testable code? Is the code sample not compiling/executing? I am afraid that this is the shortest code I could come up with to implement an attribute based call handler pipeline. Can you please elaborate or let me know what issue you are facing? Maybe something got missed while copy pasting my code segment? – Sucharith V. Oct 08 '14 at 14:45
  • Got some replies in [CodePlex Unity Discussion](https://unity.codeplex.com/discussions/569262) for the same question posted there. – Sucharith V. Oct 10 '14 at 15:24

0 Answers0