c# - 如何将自定义 IOperationInvoker 添加到端点中的每个操作

标签 c# wcf

我的服务中需要自定义 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/

相关文章:

c# - 我可以使用接口(interface)或多态性将两个不同的 xml 文件反序列化为一个对象类型吗?

c# - 如何强制使用 .Include 查询 Entity Framework 6 进行外部连接

c# - 是否可以针对多个身份验证提供程序来保护 ASP.NET Web API 2 应用程序的安全?

c# - 将关系数据库存储为 XML

c# - 在通过网络发送之前压缩位图

c# - 适用于 C# ASP.NET 开发人员的 SharePoint

c# - WCF 异步通知订阅者

wcf - Dynamics CRM - 调用者未通过服务身份验证

wcf - 如何正确扩展WCF返回的类?

silverlight - 如何检测断开的双工轮询客户端