Last active
January 11, 2021 02:04
-
-
Save cmatskas/ead6945ebca24e07f74f to your computer and use it in GitHub Desktop.
CustomMessageInspector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Security.Policy; | |
using System.ServiceModel.Channels; | |
using System.ServiceModel.Dispatcher; | |
using System.Xml; | |
namespace APIServiceConsumer.Services | |
{ | |
public class ClientMessageInspector : IClientMessageInspector | |
{ | |
public string LastRequestXml { get; private set; } | |
public string LastResponseXml { get; private set; } | |
public void AfterReceiveReply(ref Message reply, object correlationState) | |
{ | |
LastResponseXml = reply.ToString(); | |
} | |
public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel) | |
{ | |
LastRequestXml = request.ToString(); | |
var xmlPayload = ChangeMessage(); | |
var ms = new MemoryStream(); | |
var writer = new StreamWriter(ms); | |
writer.Write(xmlPayload); | |
writer.Flush(); | |
ms.Position = 0; | |
var reader = XmlReader.Create(ms); | |
request = Message.CreateMessage(reader, int.MaxValue, request.Version); | |
var payload = request.ToString(); | |
return request; | |
} | |
/// Manipulate the SOAP message | |
private string ChangeMessage() | |
{ | |
// LastRequestXML is a string here. You can change it to your heart's content | |
// Sample example here | |
// NOTE - I couldn't load the string into an XDocument or XMLDocument as it was throwing all sort of errors | |
// about undefined namespaces. String manipulation to the rescue! | |
// strip out the envelope and header | |
var startIndexOfStringToKeep = LastRequestXml.IndexOf("RequestHeader", StringComparison.CurrentCulture); | |
// strip out the footer | |
var sanitizedRequestXml = LastRequestXml.Substring(startIndexOfStringToKeep - 1); | |
var firstPhase = sanitizedRequestXml.Replace(@"</s:Envelope>", string.Empty); | |
var secondPhase = test.Replace(@"</s:Body>", string.Empty); | |
var thirdPhase = test2.Replace(@"</SomeServiceRequest>", string.Empty); | |
// wrap the body with the right element declarations sans any namespaces | |
var bodyRequestXml = "<SomeServiceRequest>" + thirdPhase + "</SomeServiceRequest>"; | |
var document = new XmlDocument(); | |
var root = document.CreateElement("s", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); | |
root.SetAttribute("xmlns:s", "http://schemas.xmlsoap.org/soap/envelope/"); | |
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); | |
root.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); | |
root.SetAttribute("xmlns", "http://www.randomnamespaceroot.co.uk/SomeServiceIdentification"); | |
document.AppendChild(root); | |
var bodyHeader = document.CreateElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); | |
root.AppendChild(bodyHeader); | |
var body = document.CreateElement("IdentifyCustomerRequest", "http://www.randomnamespaceroot.co.uk/SomeServiceIdentification"); | |
bodyHeader.AppendChild(body); | |
var replacedString = GetPayloadString(document); | |
var removedString = replacedString.Replace(@"<IdentifyCustomerRequest />", bodyRequestXml); | |
return removedString; | |
} | |
/// Helper method to get the XMLDocument format right | |
private string GetPayloadString(XmlDocument document) | |
{ | |
var settings = new XmlWriterSettings(); | |
settings.OmitXmlDeclaration = true; | |
using (var stringWriter = new StringWriter()) | |
using (var xmlTextWriter = XmlWriter.Create(stringWriter, settings)) | |
{ | |
document.WriteTo(xmlTextWriter); | |
xmlTextWriter.Flush(); | |
return stringWriter.GetStringBuilder().ToString(); | |
} | |
} | |
} | |
} |
added .cs file extension
Fixed issue with wrong class name
Added closing brackets :)
Thanks, this concept was useful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added c# language setting