我有一个MessageInspector类,它实现了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;
        }
    }
}


我已经在WCF的web.config中配置了它:

<?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>


运行wcf(在IIS中托管)时,无法在we配置中识别我的“ MessageInspector”类。

“ / WCFAuthentication”应用程序中的服务器错误。

配置错误

说明:在处理服务于此请求所需的配置文件期间发生错误。请查看下面的特定错误详细信息,并适当修改您的配置文件。

解析器错误消息:无法加载为扩展名“ MessageInspector”注册的类型“ WCFAuthentication.MessageInspector,MessageInspector,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null”。

Source Error:


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

最佳答案

您不能以这种方式直接添加MessageInspector,您需要同时实现IEndpointBehaviorBehaviorExtensionElement

MSDN has a detailed example.

10-05 22:31