如何从WCF服务器代码中读取WCF Soap响应?

时间:2015-09-04 17:56:55

标签: c# wcf

我查看了我在本网站上可以找到的所有相关帖子,并且我已经浏览了互联网...而且我很难知道如何在将WCF的响应发送回之前阅读WCF的SOAP响应。客户端。

我希望以最终形式阅读响应(包含XML中的soap信封元素)并将其发送到我的自定义日志过程。

请注意,我必须使用自定义日志功能,该功能将响应作为字符串接受并将其发送到SQL数据库(未显示)。这必须在运行时由代码处理每个响应;我不能简单地使用一些第三方记录/跟踪工具。

这是我的WCF服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using MSADataAccess;
using System.Web.Configuration;
using System.ServiceModel.Channels;

namespace svcIDIVS
{
    public class VINS : IVINS
    {

        public string VerifyInsurance(string coverageRequest)
        {
            string responseMessage = null;
            DataAccess da = new DataAccess();
            responseMessage = da.VerifyInsurance(coverageRequest);
            return responseMessage;
        }
    }
}

我需要将responseMessage及其SOAP Envelope读入字符串。看起来应该是这样的:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <VerifyInsuranceResponse xmlns="http://tempuri.org/">
      <VerifyInsuranceResult>&lt;CoverageResponseDocument PublicationVersion="00200809" PublicationDate="2008-11-05" xmlns="http://www.iicmva.com/CoverageVerification/"&gt;  
    &lt;Detail&gt;  
        &lt;PolicyInformation&gt;  
            &lt;CoverageStatus&gt;  
                &lt;ResponseDetails&gt;  
                    &lt;ResponseCode&gt;Unconfirmed&lt;/ResponseCode&gt;  
                    &lt;UnconfirmedReasonCode&gt;VIN2&lt;/UnconfirmedReasonCode&gt;  
                &lt;/ResponseDetails&gt;  
            &lt;/CoverageStatus&gt;  
            &lt;OrganizationDetails&gt;   
                &lt;NAIC&gt;14788&lt;/NAIC&gt;  
                &lt;!-- this can be echoed from the request or provide the actual NAIC that has evidence of coverage --&gt;  
            &lt;/OrganizationDetails&gt;  
            &lt;PolicyDetails&gt;  
                &lt;!-- this section can be echoed from the request --&gt;  
                &lt;VerificationDate&gt;07/16/2008 00:00:00&lt;/VerificationDate&gt;  
                &lt;PolicyKey&gt;03K85845&lt;/PolicyKey&gt;  
                &lt;PolicyState&gt;CT&lt;/PolicyState&gt;  
            &lt;/PolicyDetails&gt;  
        &lt;/PolicyInformation&gt;  
    &lt;/Detail&gt;  
&lt;/CoverageResponseDocument&gt;  
</VerifyInsuranceResult>
    </VerifyInsuranceResponse>
  </s:Body>

1 个答案:

答案 0 :(得分:1)

WCF有Message Dispatcher Inspector,您可以在发送/回复服务之前拦截邮件并进行验证。

public class BodMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            return instanceContext;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            // DO your stuff here and validate your reply Message
        }
    }