Send SOAP messages via WCF with MTOM and Content-Transfer-Encoding: 7-bit

2.4k views Asked by At

I am trying to send a SOAP message via WCF to the IRS, and it keeps getting rejected because my MTOM attachment is formatted incorrectly.

I've narrowed down the issue to my Content-Transfer-Encoding value. It is set to Binary (shorthand for 8-bit).

The IRS service wants me to use 7-bit, with an 8-bit-encoded attachment (in other words, encode with UTF-8 and then guarantee that I'm not using any non-ASCII characters).

I'm already using a custom message encoder in order to gzip my requests (responses come back plain-text, ugh). This is what my WriteMessage looks like right now.

public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) {
    // get an instance of the underlying encoder
    var encoder = new MtomMessageEncodingBindingElement() {
            MessageVersion = MessageVersion.Soap11WSAddressing10,
            WriteEncoding = System.Text.Encoding.UTF8
        }.CreateMessageEncoderFactory().Encoder;

    // write the message contents
    var uncompressed = encoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);

    // compresses the resulting byte array
    return CompressBuffer(uncompressed, bufferManager, messageOffset);
}

Any ideas? When I change the WriteEncoding property to ASCII or UTF7 .NET throws an ArgumentException and tells me the format is not supported.

2

There are 2 answers

1
Tim Schumacher On BEST ANSWER

I am using Java Apache CXF and WSS4J for the IRS solution, but if you are getting this error "The message was not formatted properly and/or cannot be interpreted. Please review the XML standards outlined in Section 3 of the AIR Submission Composition and Reference Guide located at https://www.irs.gov/for-Tax-Pros/Software-Developers/Information-Returns/Affordable-Care-Act-Information-Return-AIR-Program, correct any issues, and try again." it is because the IRS is expecting this:

Content-Type: application/xml
Content-Transfer-Encoding: 7bit
Content-ID: <6920edd2-a3c7-463b-b336-323a422041d4-1@blahurn:us:gov:treasury:irs:common>
Content-Disposition: attachment;name="1094B_Request_BBBBB_20151019T121002000Z.xml" 
4
jabtri On

It appears the built in MTOM encoder in WCF will not encode a request compatible with the IRS service. It encodes whatever it finds in the request that's base64 encoded including the BinarySecurityToken in the signed request. I was able to get a request closer to IRS requirements by creating a custom encoder. Within WriteMessage, you can append and prepend MIME separators and reencode the file as an attachment. An outgoing message inspector is required to properly set the headers: https://blogs.msdn.microsoft.com/carlosfigueira/2011/04/18/wcf-extensibility-message-inspectors/