2010-06-19 58 views
2

我需要在我的服務中自定義IOperationInvoker。我有它的工作很好,但我不特別喜歡這個想法,爲了將它應用到給定的操作,我必須使它成爲一個屬性。我似乎無法找到任何有關如何配置它的文檔(類似於端點上的MessageInspector)。如何添加自定義IOperationInvoker到端點的每個操作

我創建了一個CustomBehavior,它實現了一個IEndpointBehavior。在ApplyDispathBehavior我有以下幾點:

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
{ 
    foreach (var operation in endpointDispatcher.DispatchRuntime.Operations) 
    { 
     operation.Invoker = new MockServiceOperationInvoker() {Invoker=operation.Invoker }; 
    } 
} 

但是,自定義調用似乎並沒有「大棒」。這些更改是否會在生命週期的後期被清除?

如果我將行爲設爲屬性,那麼一切都會奏效,但我寧願不這樣做。有沒有人知道更好的方法來將自定義的OperationBInvoker應用於給定端點中的所有方法?

非常感謝!

回答

2

我想我想通了。通過循環遍歷每個操作,我可以添加一個行爲。我不知道這是否是最好的方法,但它確實有效。

public class MockOperationBehavior : BehaviorExtensionElement, IOperationBehavior, IEndpointBehavior 
{ 
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
    { 
    foreach (var op in endpoint.Contract.Operations) 
    { 
     op.Behaviors.Add(this); 
    } 
    } 
} 
+0

你會如何將它應用到ClientOperation? – 2012-05-01 13:12:32

+0

@aloneguid我在我的博客上做了一個小系列。也許這將有助於http://codeharder.com/post/Modifying-a-WCF-operation-at-runtime-%E2%80%93-part-1.aspx&http://codeharder.com/post/Modifying -a-WCF-operation-at-runtime-%E2%80%93-part-2.aspx&http://codeharder.com/post/Modifying-a-WCF-operation-at-runtime-%E2%80 %93 - 部分 - 3.aspx – Joe 2012-05-16 15:46:26

相關問題