2010-06-19 74 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

相关问题