3

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;
    }

?

2 Answers2

1

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.

Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
  • I know this code do not work. I wrote this to describe my idea. All I need - to pass parameter from resolve-calling code to ICallHandler implementation. Also I found that it is possible somehow to pass parameter to ICallHandler implementation constructor. But ICallHandler contructor calls only once per container (as I tested). But I need pass parameter avery time I call Resolve<>(). – Pavels Ahmadulins Jul 18 '11 at 10:00
  • What is the nature of the `message`? Is it something you know at design-time? – Steve Wilkes Jul 18 '11 at 10:15
  • In my example it is **string**. In my real world application it is **System.Web.Services.WebService**. My aspect trying to preform preparations with instance of **WebService** before real logic will be called. – Pavels Ahmadulins Jul 18 '11 at 11:22
  • I see. To be honest, this doesn't sound possible using interception. You could do it with a decorator instead? – Steve Wilkes Jul 18 '11 at 14:17
  • Please feel free to email me if you'd like to discuss this in more depth :) – Steve Wilkes Jul 18 '11 at 14:52
1

It seems, that Steve Wilkes was right: "To be honest, this doesn't sound possible using interception."

http://unity.codeplex.com/discussions/265679