2

I am trying to intercept a WCF client. But the Unity interceptor does not seem to work.

  1. I am not getting a call to the proxy.
  2. I did not know how to link the attribute to the container
  3. In AddPolicy what is the name attribute - Here I used the function to intercept - GetXml.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
public class GetXmlCallHandlerAttribute : HandlerAttribute
{
  public GetXmlCallHandlerAttribute(){}
  public override ICallHandler CreateHandler(IUnityContainer ignored)
  {
    return new GetXmlTestCallHandler();
  }
}
public class GetXmlTestCallHandler : ICallHandler
{
  public int Order { get; set; }  
  public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
  {
    IMethodReturn msg;
    Object returnValue = XElement.Load("../../Resources/a1.xml");
    Object[] outputs = null;
    msg = input.CreateMethodReturn(returnValue, outputs);
    return msg;        
  }
} 
class SendDataTest
{
  public void GetXmlTest()
  {
    IUnityContainer container = new UnityContainer();
    ICallHandler myInterception = new GetXmlTestCallHandler();    
    container.AddNewExtension<Interception>();
    container.RegisterType<SendData>(new Interceptor(new TransparentProxyInterceptor()));
    container.Configure<Interception>().AddPolicy("GetXml").AddCallHandler(myInterception);
    SendData target = container.Resolve<SendData>();
    XElement expected = null; // TODO: Initialize to an appropriate value
    XElement actual;
    actual = target.GetXml();
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
  }
}
onof
  • 17,167
  • 7
  • 49
  • 85
  • Does SendData extends MarshalByRefObject? It is needed to work with TransparentProxyInterceptor. – onof Oct 18 '12 at 20:58

1 Answers1

1

There are a few things you need to do: -

You need to add the InterceptionBehaviour() when registering SendData e.g.

    container.RegisterType<SendData>(new InterceptionBehaviour<PolicyInjectionBehaviour>(), new Interceptor(new TransparentProxyInterceptor()));

This is so that Unity knows to take part in any policies that you have created. The TransparentProxyInterceptor just says how to actually perform interception.

You also need to apply your attribute on the appropriate method that you want to intercept - I assume that you have done that on the GetXml method of your SendData class.

Isaac Abraham
  • 3,422
  • 2
  • 23
  • 26