Unity 1.2 - How to override policy injection settings on parent container?

Nov 8, 2011 at 7:13 AM

Hi,

I have a scenario wherein I need to override the policy injection settings on parent container from the child container. I was expecting that specifying the same policy name would do the trick but it is not happening since the policy injection is applied twice. My test solution can be found here.

Thanks in advance!

 

 

 

Nov 14, 2011 at 2:13 PM

Anyone can help me pls? I've been stuck finding a workaround on this behavior. 

Nov 14, 2011 at 2:43 PM

The code below works for Unity 2.0. I could not find my way through your code sample in a reasonable time. If I missed something please come up with a stripped-down sample that shows the error.

[TestMethod]
public void TestMethod1()
{
  var parent = new UnityContainer();
  parent.AddNewExtension<Interception>();
  parent.RegisterType<ICalculator, Calculator>(
    new InterceptionBehavior<PolicyInjectionBehavior>(),
    new Interceptor<InterfaceInterceptor>());
  parent.Configure<Interception>()
    .AddPolicy("one")
    .AddMatchingRule(new MemberNameMatchingRule(new[] { "Sum" }))
    .AddCallHandler<Return100>();
  var fromParent = parent.Resolve<ICalculator>();
  int result = fromParent.Sum(1, 2);
  Assert.AreEqual(100, result);
  var child = parent.CreateChildContainer();
  child.AddNewExtension<Interception>();
  child.RegisterType<ICalculator, Calculator>(
    new Interceptor<InterfaceInterceptor>(),
    new InterceptionBehavior<PolicyInjectionBehavior>());
  child.Configure<Interception>()
    .AddPolicy("one")
    .AddMatchingRule(new MemberNameMatchingRule(new[] { "Sum" }))
    .AddCallHandler<Return200>();
  var fromChild = child.Resolve<ICalculator>();
  result = fromChild.Sum(1, 2);
  Assert.AreEqual(200, result);
}

public class Return200 : ICallHandler
{
  public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
  {
    return input.CreateMethodReturn(200);
  }
  public int Order { get; set; }
}
public class Return100 : ICallHandler
{
  public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
  {
    return input.CreateMethodReturn(100);
  }
  public int Order { get; set; }
}
public class Calculator : ICalculator
{
  public int Sum(int x, int y)
  {
    return x + y;
  }
  public int Sub(int x, int y)
  {
    return x - y;
  }
}
public interface ICalculator
{
  int Sum(int x, int y);
  int Sub(int x, int y);
}

Nov 21, 2011 at 2:54 AM

Hi daaberkads,

Sorry for the delay in responding.  I was able to reproduce your issue under Unity 1.2.  I tried everything I could think of to try to work around that behavior (different interceptors, runtime configuration, design time configuration, different policies etc.) but could not arrive at a viable work around.

Since the above code seems to be working (although I had to use a TransparentProxyInterceptor for some reason) I would recommend upgrading to Unity 2.0 to resolve the issue.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com