IClientMessageInspector not working in WCF

4.8k Views Asked by At

I have a class MessageInspector which implements IClientMessageInspector.

namespace WCFAuthentication
{
    public class MessageInspector : IClientMessageInspector
    {

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            //throw new NotImplementedException();
            Debug.WriteLine("IClientMessageInspector.AfterReceiveReply called.");
            Debug.WriteLine("Message: {0}", reply.ToString());
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            //throw new NotImplementedException();
            Debug.WriteLine("IClientMessageInspector.BeforeSendRequest called.");
            return null;
        }
    }
}

I have configured this in my web.config of WCF :

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add
          name="MessageInspector"
          type="WCFAuthentication.MessageInspector, MessageInspector, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp/>
        </behavior>
        <behavior name="restfulBehavior_jwt">
          <webHttp/>
          <MessageInspector/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name ="WCFAuthentication.Service1">
        <endpoint address="" behaviorConfiguration="restfulBehavior_jwt" binding="webHttpBinding" contract="WCFAuthentication.IService1"/>
      </service>
      <service name ="WCFAuthentication.Authentication">
        <endpoint address="" behaviorConfiguration="restfulBehavior" binding="webHttpBinding" contract="WCFAuthentication.IAuthentication"/>
      </service>
    </services>


    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

While running wcf ( hosted in IIS), it is not able to recognize my "MessageInspector" class in the we config.

Server Error in '/WCFAuthentication' Application.

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: The type 'WCFAuthentication.MessageInspector, MessageInspector, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' registered for extension 'MessageInspector' could not be loaded.

Source Error: 


Line 32:         <behavior name="restfulBehavior_jwt">
Line 33:           <webHttp/>
Line 34:           <MessageInspector/>
Line 35:         </behavior>
Line 36:       </endpointBehaviors>
1

There are 1 best solutions below

0
On

You can't add your MessageInspector directly that way, you need to implement an IEndpointBehavior as well as a BehaviorExtensionElement.

MSDN has a detailed example.