2012-10-17 157 views
1

我想攔截一個WCF客戶端。但是Unity攔截器似乎不起作用。Unity攔截器 - TransparentProxyInterceptor

  1. 我沒有收到對代理的調用。
  2. 我不知道如何將屬性鏈接到容器
  3. 在AddPolicy中名稱屬性是什麼 - 在這裏我使用了攔截函數GetXml。註冊送出數據例如當

    您需要添加InterceptionBehaviour() - :

[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."); 
    } 
} 
+0

SendData是否擴展了MarshalByRefObject?需要使用TransparentProxyInterceptor。 – onof

回答

1

有你需要做的幾件事

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

這樣,Unity就知道參與您創建的任何策略。 TransparentProxyInterceptor只是說如何實際執行攔截。

您還需要將您的屬性應用於您要攔截的適當方法 - 我假設您已經在SendData類的GetXml方法上完成了該操作。