2011-09-28 65 views
2

我可以使用行为来附加IParameterInspector到ClientRuntime中的每个操作,也可以附加到服务端DispatchRuntime中的每个操作。但似乎这只能从客户端到服务。如何在Callback方向上扩展WCF?

我也希望能够在服务回调中附加一个IParameterInspector到客户端,如上所示,但是我找不到任何可扩展点来做到这一点。

任何想法?

回答

3

这是一个有点模糊,似乎并没有很好的记录,但你可以使用标准的WCF行为功能定制两端。

在客户端上,该属性会使其发生。

public class InspectorBehaviorAttribute : Attribute, IEndpointBehavior 
{ 
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
    { 
    } 

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
    { 
     foreach (var item in clientRuntime.CallbackDispatchRuntime.Operations) 
     { 
      item.ParameterInspectors.Add(ParameterInspector.Instance); 
     } 
    } 

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) 
    { 
    } 

    public void Validate(ServiceEndpoint endpoint) 
    { 
    } 
} 

只需将此属性应用于实现您的回调接口的类。

在服务器上,它有点棘手。您需要通过ApplyDispatchBehavior进行连接。在这种情况下,我通过服务行为完成了它,但主体也适用于OperationBehaviors和EndpointBehaviors。

public class InspectorBehaviorAttribute : Attribute, IServiceBehavior 
{ 
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
    { 
    } 

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (var item in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>()) 
     { 
      foreach (var ep in item.Endpoints) 
      { 
       foreach (var op in ep.DispatchRuntime.CallbackClientRuntime.Operations) 
       { 
        op.ParameterInspectors.Add(ParameterInspector.Instance); 
       } 
      } 
     } 
    } 

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
    } 
} 

再次,只需将此属性应用于您的服务实现,让您的参数检查器用于所有回调操作。

尽管这些示例演示了连接IParameterInspector实现,但对于所有其他WCF扩展点的相同方法可用于在客户端和服务器上自定义回调通道。

+0

谢谢你,CallbackDispatchRuntime和CallbackClientRuntime是我之前的,非常感谢。 –