简体   繁体   English

如何从WCF服务器代码读取WCF肥皂响应?

[英]How to read WCF Soap Response from WCF Server Code?

I have looked through all of the related posts I could find on this site and I have scoured the Internet...and I am stumped about how to read a WCF's SOAP response before it sends it back to the client. 我浏览了我在该站点上可以找到的所有相关文章,并搜寻了Internet ...在如何将WCF的SOAP响应发送回客户端之前,我感到很困惑。

I want to read the response in its final form (with the soap envelop elements included in the XML) and send it to my custom log procedure. 我想阅读最终形式的响应(带有XML中包含的soap envelop元素),并将其发送到我的自定义日志过程。

Note that I have to use a custom log function provided that accepts the response as a string and sends it to a SQL db (not shown). 请注意,我必须使用提供的自定义日志功能,该功能接受响应作​​为字符串并将其发送到SQL db(未显示)。 This has to be handled by the code at runtime for each response; 对于每个响应,必须在运行时由代码处理; I can't simply use some 3rd party logging/tracer tool. 我不能简单地使用一些第三方记录/跟踪工具。

Here is my WCF service code: 这是我的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;
        }
    }
}

I need to read responseMessage along with its SOAP Envelope into a string. 我需要将responseMessage及其SOAP Envelope读入字符串。 It should look something like this: 它看起来应该像这样:

<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>

WCF has Message Dispatcher Inspector wherein you can intercept messages and validate prior to send/reply for your service. 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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM