Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MTOM attachments #43

Open
jebbench opened this issue Nov 21, 2018 · 2 comments · May be fixed by #44
Open

Support MTOM attachments #43

jebbench opened this issue Nov 21, 2018 · 2 comments · May be fixed by #44

Comments

@jebbench
Copy link

I need support to receiving MTOM attachments from a service I'm calling; I'm going to look at submitting a PR for this.

Any advice on where to start implementing this? My current thinking is to add an Attachments property to the SoapEnvelope and to intercept multipart responses in SoapClient.SendAsync; extract the attachments and add them to the envelop and then continue processing the SOAP content as happens now.

Cheers,
JB

@gravity00
Copy link
Owner

Hi @jebbench,

I've never used MTOM so I can't give you a good input right now, but from a fast analysis, it's seems to be tied to the communications protocol, not SOAP by itself, so I'm very reluctant to add support in the SoapEnvelope.

Let me dig deeper into this so I can give better input but if you need to support this fast, my best recommendation is to use the OnHttpRequestAsyncAction and OnHttpResponseAsyncAction with closures to manipulate the HTTP requests and responses.

@jebbench
Copy link
Author

I've started having a play with this; you're right that it uses the http protocol to transmit the attachments rather than placing them in the SOAP envelope payload itself but I'd argue that the attachements are part of the SOAP message.

https://www.w3.org/TR/soap12-mtom/ give a good overview of MTOM.

I could use the OnHttpXXX handlers to at least process multipart request/responses but how would I go about associating the attachments from each request/response with a SOAP request without creating separate OnHttpXXX handlers (and SoapClient) for each request I make?

I've currently dumped a bit of code into the middle of SoapClient to prove the mechanics; I'll tidy it up and move it to a handler if possible:

var beforeHttpRequestHandlersResult =
    await RunBeforeHttpRequestHandlers(
        requestXml, url, action, trackingId,
        beforeSoapEnvelopeSerializationHandlersResult.State, handlersOrderedAsc, ct);

var response =
    await HttpClient.SendAsync(beforeHttpRequestHandlersResult.Request, ct).ConfigureAwait(false);

IDictionary<string, HttpContent> attachments = new Dictionary<string, HttpContent>();
if (response.Content.IsMimeMultipartContent())
{
    var multipart = await response.Content.ReadAsMultipartAsync(ct);
    foreach (var content in multipart.Contents)
    {
        if (content.Headers.ContentType.MediaType == "application/xop+xml")
        {
            // This is the SoapEnvelope
            response.Content = content;
        }
        else
        {
            var cidMatch = _cidRegex.Match(content.Headers.GetValues("Content-Id").First());
            if (cidMatch.Success)
            {
                attachments.Add(cidMatch.Groups[1].Value, content);
            }
        }
    }
}

var handlersOrderedDesc = _handlers.OrderByDescending(e => e.Order).ToList();

var afterHttpResponseHandlersResult =
    await RunAfterHttpResponseHandlers(
        response, url, action, trackingId, beforeHttpRequestHandlersResult.State, handlersOrderedDesc, ct);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants