Search code examples
c#unity-containerinterceptionunity-interception

Unity Interception: How to pass parameter into ICallHandler implementation?


Can I pass message parameter to ICallHandler implementation like this:

var logic = container.Resolve<IBussinessLogic>(message);

And use it like this:

IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine(
            string.Format(
                "Begin {0} with param {1}",
                input.MethodBase.Name, 
                message // parameter I need to be passed
            )
        );

        var result = getNext.Invoke()(input, getNext);

        Console.WriteLine("End " + input.MethodBase.Name);
        return result;
    }

?


Solution

  • The message you're passing to the Resolve method is actually the named instance name for Unity to construct. This value is used by Unity to select which implementation of IBusinessLogic to use; after construction of the implementing object it is lost.

    This value is therefore only within Unity during the object's construction; your ICallHandler cannot access it as you cannot intercept constructors.