麻烦让IClientMessageInspector工作

时间:2013-12-17 19:07:12

标签: wcf

这是Stack Overflow主题The equivalent of Fiddler SOAP Request/Response in the Database的延续。我实现了一个ClientMessageInspector,如下所示:

 public class RREMOutputMessageInspector : IClientMessageInspector
    {
        private string _connStringProd = String.Empty;
 public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
            reply = buffer.CreateMessage();

            _connStringProd = ConfigurationManager.ConnectionStrings["RREM_Gilbane_ProdConnectionString"].ToString();
            NREMGilbaneTableAdapters.SOAPMessagesTableAdapter soapMessages =
                new NREMGilbaneTableAdapters.SOAPMessagesTableAdapter();
            soapMessages.Connection.ConnectionString = _connStringProd;

            string replyString = buffer.CreateMessage().ToString();
            soapMessages.Insert(replyString, DateTime.Now, "Interface12", "Interface12", "", "", "", "");
        }

        public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
        {
            MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
            request = buffer.CreateMessage();

            _connStringProd = ConfigurationManager.ConnectionStrings["RREM_Gilbane_ProdConnectionString"].ToString();
            NREMGilbaneTableAdapters.SOAPMessagesTableAdapter soapMessages =
                new NREMGilbaneTableAdapters.SOAPMessagesTableAdapter();
            soapMessages.Connection.ConnectionString = _connStringProd;

            string requestString = buffer.CreateMessage().ToString();
            soapMessages.Insert(requestString, DateTime.Now, "Interface12", "Interface12", "", "", "", "");
            return null;
        }

然后我为检查员实现了一种行为:

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

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            RREMOutputMessageInspector inspector = new RREMOutputMessageInspector();
            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();
        }

最后在我的WCF服务中,在调用其他Web服务时,我有以下内容:

 documentSvcResponse = client.retrieveDocument(authHeader, retrieveDoc);
                    client.Endpoint.Behaviors.Add(new RREMBehavior());

我的Inspector中没有遇到任何断点。您是否应该能够在检查员中设置断点。此外,此Web服务还实现了一个循环,其中上述两行正在多次执行。那是问题吗?我没有看到这个有用的证据,请再次帮助!

1 个答案:

答案 0 :(得分:0)

是的,你应该。您是否已将endpointbehavior和扩展添加到web.config文件中?看看这篇msdn文章:http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector(v=vs.110).aspx

我真的不明白你关于循环的问题?但这听起来有点奇怪。