无法使ClientMessageInspector与Config一起使用

时间:2014-01-03 18:13:46

标签: wcf

我编写了一个ClientMessageInspector,它是在我可以开始工作的示例Message Inspection in WCF之后建模的。我有一个服务,是其他服务的客户端。它位于“Interface12ServiceProject”项目中。 MessageInspector是:

namespace MessageListener.Instrumentation
{
    public class MessageInspector : IClientMessageInspector
    {
        private const string LogDir = @"C:\Logs\Interface12Service\";

        private Message TraceMessage(MessageBuffer buffer)
        {
            // Must use a buffer rather than the original message, because the Message's body can be processed only once.
            Message msg = buffer.CreateMessage();

            //Setup StringWriter to use as input for our StreamWriter
            //This is needed in order to capture the body of the message, because the body is streamed
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
            msg.WriteMessage(xmlTextWriter);
            xmlTextWriter.Flush();
            xmlTextWriter.Close();

            //Setup filename to write to
            if (!Directory.Exists(LogDir))
                Directory.CreateDirectory(LogDir);

            DateTime now = DateTime.Now;
            string datePart = now.Year.ToString() + '-' + now.Month.ToString() + '-' + now.Day.ToString() + '-' + now.Hour + '-' + now.Minute + '-' + now.Second;
            string fileName = LogDir + "\\" + datePart + '-' + "SoapEnv.xml";

            //Write to file
            using (StreamWriter sw = new StreamWriter(fileName))
                sw.Write(stringWriter.ToString());

            //Return copy of origonal message with unalterd State
            return buffer.CreateMessage();
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            reply = TraceMessage(reply.CreateBufferedCopy(int.MaxValue));
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            request = TraceMessage(request.CreateBufferedCopy(int.MaxValue));
            return null;
        }
    }
}

然后我有一个LoggingEndpointBehavior:

namespace MessageListener.Instrumentation
{
    public class LoggingEndpointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            throw new NotImplementedException();
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            MessageInspector inspector = new MessageInspector();
            clientRuntime.MessageInspectors.Add(inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            throw new NotImplementedException();
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            throw new NotImplementedException();
        }
    }
}

然后是LoggingBehaviorExtenhsionElement:

namespace MessageListener.Instrumentation
{
    public class LoggingBehaviorExtensionElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof (LoggingEndpointBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new LoggingEndpointBehavior();
        }
    }
}

最后,相关配置是:

       <client>
        <endpoint address="..."
                binding="basicHttpBinding" bindingConfiguration="Tier2DocumentEndpointImplServiceSoapBinding"
                contract="SSHIPProdInterface12.Tier2DocumentEndpoint" name="Tier2DocumentEndpointImplPort" behaviorConfiguration="messageInspectorBehavior"/>

    <endpointBehaviors>
            <behavior name="messageInspectorBehavior">
              <messageInspector />
            </behavior>
          </endpointBehaviors>
        </behaviors>
<extensions>
      <behaviorExtensions>
        <add name="messageInspector" type="MessageListener.Instrumentation.LoggingBehaviorExtensionElement, Interface12ServiceProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>

当我运行Interface12Service并尝试使用附加的endpointBehavior调用其他服务时:

serviceResponse = client.getTier2Document(authHeader, arg1);

我得到“方法或操作未实现”异常,我在MessageInspector中的断点永远不会变热。当我取消endpointBehavior时,对其他服务的调用成功但没有消息检查。我究竟做错了什么?我可以用这种方式将我的行为附加到服务上吗?

1 个答案:

答案 0 :(得分:1)

我通过查看堆栈异常找到了答案:Validate抛出NotImplementedException

相关问题