我有一个类MessageInspector,它实现IClientMessageInspecter。
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"应用程序中的服务器错误。
配置错误
描述:处理服务此请求所需的配置文件时出错。请查看下面的具体错误详细信息,并适当修改您的配置文件。
分析程序错误消息:类型"WCFAuthentication"。无法加载为扩展名"MessageInspector"注册的MessageInspecter、MessageInspectr、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提供了一个详细的示例。