title: WCF - How to invoke Message Inspector before Service Authorization Manager?
body: <p>We are using Message Inspector to customize the SOAP message by adding some information at client side and retrieving the added information at the server side.
  We are also using Custom Authorization Manager by using ServiceAuthorizationManager to use retrieved SOAP based Message information.</p>
  
  <p>To customize the SOAP messages, we are overriding two methods:</p>
  
  <p>a) BeforeSendRequest (Client Side) - This method is used to customize the SOAP message header in Message Inspector.</p>
  
  <pre><code>public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
           Dictionary&lt;string,string&gt; headerInfo = new Dictionary&lt;string,string&gt;();
  
           headerInfo.Add("UserId","1111");
  
           MessageHeader header = MessageHeader.CreateHeader("LocalName", "NamespaceURI", headerInfo);
           request.Headers.Add(header);
  
           return null;
        }
  </code></pre>
  
  <p>b) AfterReceiveRequest (Server Side) - This method is used to fetch the customized SOAP message in Message Inspector.</p>
  
  <pre><code>public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
           Dictionary&lt;string, string&gt; headerInfo = request.Headers.GetHeader&lt;Dictionary&lt;string, string&gt;&gt;("LocalName", "NamespaceURI");
           return null;
        }
  </code></pre>
  
  <p>Now, when request is made from the client side then first call is being made in Custom Authorization Manager class instead of AfterReceiveRequest() in Message Inspector at Server side.</p>
  
  <p>We have registered our Custom Authorization Manager in the App.config file as shown:</p>
  
  <pre><code>&lt;serviceBehaviors&gt;
  &lt;behavior name="SampleAuthorizationService.Service1Behavior"&gt;
     &lt;serviceMetadata httpGetEnabled="false"/&gt;
     &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
     &lt;serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="SampleAuthorizationSecurity.CustomAuthorizationManager, SampleAuthorizationSecurity"&gt;
        &lt;authorizationPolicies&gt;
           &lt;add policyType="SampleAuthorizationSecurity.CustomAuthPolicy, SampleAuthorizationSecurity"/&gt;
        &lt;/authorizationPolicies&gt;
     &lt;/serviceAuthorization&gt;
  &lt;/behavior&gt;
  &lt;/serviceBehaviors&gt;
  </code></pre>
  
  <p>The flow should be from Message Inspector to Custom Authorization Manager at server side. But, in our case the flow is exactly opposite i.e. from Custom Authorization Manager to Message Inspector.
  This might be happening due to Registration of Custom Authorization Manager in the App.config.</p>
  
  <p>Can anyone help me out to change the flow from Message Inspector to Custom Authorization Manager at server side?</p>