wcfc#-4.0servicebehavioridispatchmessageinspector

IServiceBehavior : prevent applying Message inspectors for some messages


On a WCF Service, I have added an attribute [WebAppServiceBehavior] which checks for some headers in messages of the service for authenticity.

Is it possible what I could use other attributes on some specific methods that can ignore these checks.

My problem is I have 20 methods in a service & I want to exclude only 2 methods from this check.

[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)
        {

        }
    }

Solution

  • I added an attribute [IgnoreValidation] on methods for which i had to ignore the checking.

      public class IgnoreValidation : Attribute
        {
    
        }
    

    From Above Eg: in Question :

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

    & in the Inspector, I added this code for checking whether the operation has the attribute.

     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;
                        }
                    }
                }