我的服务中需要自定义 IOperationInvoker。我让它工作得很好,但我并不特别喜欢这样的想法,即为了将它应用于给定的操作,我必须将它设为一个属性。我似乎找不到任何关于如何基于它进行配置的文档(类似于端点上的 MessageInspector)。
我创建了一个实现 IEndpointBehavior 的 CustomBehavior。在 ApplyDispathBehavior 中,我有以下内容:
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (var operation in endpointDispatcher.DispatchRuntime.Operations)
{
operation.Invoker = new MockServiceOperationInvoker() {Invoker=operation.Invoker };
}
}
但是,自定义调用程序似乎并没有“坚持”。能否在生命周期的后期清除更改?
如果我将行为作为一个属性,一切都会起作用,但我宁愿不这样做。有谁知道将自定义 OperationBInvoker 应用于给定端点中所有方法的更好方法?
非常感谢!
最佳答案
我想我明白了。通过遍历每个操作,我可以为其添加一个行为。我不知道这是否是最好的方法,但它确实有效。
public class MockOperationBehavior : BehaviorExtensionElement, IOperationBehavior, IEndpointBehavior
{
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (var op in endpoint.Contract.Operations)
{
op.Behaviors.Add(this);
}
}
}
关于c# - 如何将自定义 IOperationInvoker 添加到端点中的每个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3073999/