【问题标题】:IServiceBehavior : prevent applying Message inspectors for some messagesIServiceBehavior : 阻止对某些消息应用消息检查器
【发布时间】:2016-07-25 00:10:39
【问题描述】:

在 WCF 服务上,我添加了一个属性 [WebAppServiceBehavior],用于检查服务消息中的某些标头的真实性。

是否有可能我可以在某些可以忽略这些检查的特定方法上使用其他属性。

我的问题是我的服务中有 20 种方法,而我只想从该检查中排除 2 种方法。

[WebAppServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SyncService : ISyncService
    {
        public void DoWork() //check here 
        {

        }


public void DoWork2()//ignore here
        {

        }

}


  public class WebAppServiceBehavior : 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)
        {
            // throw new NotImplementedException();

            foreach (ChannelDispatcher ch in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endptDispatcher in ch.Endpoints)
                {
                    endptDispatcher.DispatchRuntime.MessageInspectors.Add(new WebAppServiceMessageInspector());
                }
            }
        }

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

  public class WebAppServiceMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];



                 HttpRequestMessageProperty httpProp = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        object operationName;
        request.Properties.TryGetValue(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName, out operationName);
        if (httpProp != null && operationName != null && operationName.ToString().ToLower() == "options".ToLower())
        {   
                return "Options";            
        }



            /*if (ISValid Login ) //checking here For a specific header & returning error or success.
            {
                return instanceContext;
            }
            else
            {
                throw new FaultException("Invalid Authorization Code" + Ac.ErrorMsg);
            }*/
return instanceContext;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {

        }
    }

【问题讨论】:

    标签: wcf c#-4.0 servicebehavior idispatchmessageinspector


    【解决方案1】:

    我在必须忽略检查的方法上添加了一个属性 [IgnoreValidation]。

      public class IgnoreValidation : Attribute
        {
    
        }
    

    从上面例如:在问题中:

    [WebAppServiceBehavior]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class SyncService : ISyncService
        {
            public void DoWork() //check here 
            {
    
            }
    
     **[IgnoreValidation]**
    public void DoWork2()//ignore here
            {
    
            }
    
    }
    

    & 在 Inspector 中,我添加了这段代码用于检查操作是否具有属性。

     string actionName = Convert.ToString(operationName);
                if (!string.IsNullOrEmpty(actionName))
                {
                    var methodInfo = instanceContext.Host.Description.ServiceType.GetMethod(actionName);
                    if (methodInfo != null)
                    {
                        var customAttributes = methodInfo.GetCustomAttributes(false);
                        if (customAttributes.Any(ca => ca.GetType().Equals(typeof(IgnoreValidation))))
                        {
                            return instanceContext;
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多