簡體   English   中英

WCF服務客戶端:未在線路上發送自定義肥皂信封

[英]WCF service client: Customized soap envelope not being sent on the wire

我從WCF客戶端調用第三方Web服務。 問題是該服務需要特定的肥皂信封格式。

WCF Soap格式問題處理同樣的問題,建議的解決方案是編寫CustomTextMessageEncoder。

我編寫了一個自定義消息編碼器,並在WriteMessage方法中完成我的自定義格式化,采用MSDN示例編碼器,如主題http://msdn.microsoft.com/en-us/library/ms751486(v=vs.110)中所述.aspx

自定義格式刪除soap標頭內容並添加第三方服務所需的一些名稱空間前綴。

我可以看到在VS調試器中調用編碼器並且它正在產生所需的輸出:

        public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
    {
        // code from MSDN example
        MemoryStream stream = new MemoryStream();
        XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
        message.WriteMessage(writer);
        writer.Close();

        // My customizing code
        var targetStream = new MemoryStream();
        FormatStreamToMirthFormat(stream, targetStream);

        // code from MSDN example
        // put the contents of the stream into a byte array
        byte[] messageBytes = targetStream.GetBuffer();
        int messageLength = (int)targetStream.Position;
        stream.Close();

        int totalLength = messageLength + messageOffset;
        byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
        Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);

        ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
        return byteArray;
    }

我已經在app.config中配置了服務消息跟蹤,以查看實際發送的內容以及我遇到的問題是,看來傳入的soap信封仍在線路上發送(使用basicHttpBinding時發送的信封) 。

為什么輸入的肥皂信封而不是我的自定義肥皂信封被發送?

我注意到的另一件事是,如果我使用默認綁定“basicHttpBinding”,則線路上的消息包含一個Http標頭,后跟soap信封:

<HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
   <Method>POST</Method>
   <QueryString></QueryString>
   <WebHeaders>
       <VsDebuggerCausalityData>uIDPo+hLJO6ZB9hNt7/+pVZglrYAAAAAus8ogaD9Y0O0ThkjxtbdzBlxO8Yn2yBJggkv3BRx/qsACQAA</VsDebuggerCausalityData>
</WebHeaders>
</HttpRequest>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">..

而我的信息始於肥皂信封。

<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">

謝謝你的幫助。

MessageEncoder在WCF管道的后期發生,如果需要實現其他安全性要求,將來可能會遇到問題。 我推薦一個MessageFormatter。 看看這篇文章

它解釋了如何執行此服務器端,但您也可以通過重寫ApplyClientBehavior而不是ApplyDispatchBehavior,以及導出IClientMessageFormatter而不是IDispatchMessageFormatter,以類似的方式在客戶端執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM