c# - IClientMessageInspector 在 WCF 中不起作用

标签 c# wcf iclientmessageinspector

我有一个实现 IClientMessageInspector 的 MessageInspector 类。

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 中)时,它无法识别我们配置中的“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,您需要实现 IEndpointBehavior 以及 BehaviorExtensionElement

MSDN has a detailed example.

关于c# - IClientMessageInspector 在 WCF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29875619/

相关文章:

c# - 如何使用 C# 使表单表现得像 Windows 任务栏

wcf - 通过 WCF 公开实体

wcf - 在多个环境中测试 Web 服务

c# - 在输出窗口中打印堆栈跟踪

c# - 如何将 Silverlight Toolkit DataGrid 单元格值绑定(bind)到 TextBlock 或 TextBox 文本属性

c# - 是我的彩票程序错了吗?还是我这么倒霉?

c# - WCF 服务应用程序配置

c# - 如何在 IClientMessageInspector 中获取调用的操作名称?

.net - 如何在 BeforeSendRequest 中取消请求