簡體   English   中英

IServiceBehavior:阻止對某些消息應用消息檢查器

[英]IServiceBehavior : prevent applying Message inspectors for some messages

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

        }
    }

我在必須忽略檢查的方法上添加了屬性[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
        {

        }

}

&在檢查器中,我添加了此代碼以檢查操作是否具有屬性。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM