"c#" "core" SOAP MTOM
public static MtomObject MtomCall(this string s, string endPoint, string soapMethod)
{
var client = new HttpClient();
List<MtomAttachment> enumerable;
byte[] firstOrDefault;
using (var requestMessage =
new HttpRequestMessage(HttpMethod.Post, endPoint))
{
requestMessage.Content = new StringContent(s, Encoding.UTF8);
requestMessage.Headers.Add("ContentType", "application/xop+xml;charset=utf-8;type=\"text/xml\"");
requestMessage.Headers.Add("MIME-Version", "1.0");
requestMessage.Headers.Add("SOAPAction", soapMethod);
var multipartMemoryStreamProvider = client.SendAsync(requestMessage).Await().Content.ReadAsMultipartAsync().Await();
firstOrDefault = multipartMemoryStreamProvider.Contents.FirstOrDefault()?.ReadAsByteArrayAsync().Await();
enumerable = multipartMemoryStreamProvider.Contents.Skip(1).Select(
//solution dependent
x=> new MtomAttachment { Bytes=x.ReadAsByteArrayAsync().Await(),ContentId = x.Headers.FirstOrDefault(y => y.Key=="Content-ID").Value.FirstOrDefault()?.Replace("<","").Replace(">", "") }).ToList();
}
return new MtomObject {
Body = System.Text.Encoding.UTF8.GetString(firstOrDefault),
Attachments = enumerable };
}
public class MtomObject
{
internal string Body { get; set; }
internal List<MtomAttachment> Attachments{get;set; }
}
public partial class MtomAttachment
{
internal byte[] Bytes { get; set; }
internal string ContentId {get;set; }
}
public static class Awaitable
{
public static T Await<T>(this Task<T> task, int? timeout = null)
{
if (timeout != null)
{
task.Wait(new TimeSpan(0, 0, 0, 0, (int)timeout));
}
else
{
task.Wait();
}
return task.Result;
}
}