代码之家  ›  专栏  ›  技术社区  ›  Andy McCluggage hunter

WCF同一IParameterInspector用于服务上的所有操作

  •  13
  • Andy McCluggage hunter  · 技术社区  · 14 年前

    我已经实现了一个自定义 IParameterInspector 我想让它为我服务的每一个操作执行。

    IParameterInspector公司 实现只能与 IOperationBehavior 操作行为 实现只能用于使用属性修饰单个操作。

    有人知道我有没有办法登记 IParameterInspector公司

    1 回复  |  直到 14 年前
        1
  •  14
  •   Community CDub    8 年前

    多亏了 this 随后 this ,我找到了我要找的东西。

    IParameterInspector IOperationBehavior 水平。他们可以在 IServiceBehavior 水平。在服务级别 ApplyDispatchBehavior

    全班同学。。。

    [AttributeUsage(AttributeTargets.Class)]
    public class ServiceLevelParameterInspectorAttribute : Attribute, IParameterInspector, IServiceBehavior
    {
        public object BeforeCall(string operationName, object[] inputs)
        {
           // Inspect the parameters.
            return null;
        }
    
        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
        {
        }
    
        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
    
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
            {
                if (channelDispatcher == null)
                {
                    continue;
                }
    
                foreach(var endPoint in channelDispatcher.Endpoints)
                {
                    if (endPoint == null)
                    {
                        continue;
                    }
    
                    foreach(var opertaion in endPoint.DispatchRuntime.Operations)
                    {
                        opertaion.ParameterInspectors.Add(this);
                    }
                }
            }
        }
    }