Example: Use a different character encoding

In this example scenario we will add support for using a different character encoding in the contents of SOAP requests.

Out of the box, Windows Communication Framework (WCF) — the underlying .NET framework used to make SOAP requests — only supports the following character encodings: UTF-8, UTF-16 and Big Endian Unicode. If you try to use other encodings you will probably face one of the following runtime errors:

FailedSystem.ServiceModel.ProtocolException: The content type text/xml;
charset=ISO-8859-1 of the response message does not match the content
type of the binding (text/xml; charset=utf-8). If using a custom encoder,
be sure that the IsContentTypeSupported method is implemented properly.

Or:

System.ServiceModel.ProtocolException: There is a problem with the XML
that was received from the network. See inner exception for more details.
---> System.Xml.XmlException: The encoding in the declaration
'iso-8859-1' does not match the encoding of the document 'utf-8'.

3. Create some helper classes by copying the code presented below and pasting it in the [yourExtensionName].cs file, for example below the Css[yourExtensionName] class definition.
Note: For more information on how these classes fit the WCF architecture, check the Microsoft page mentioned above.

4. Add the code below, replacing the MssSetEncoding function placeholder that Integration Studio created for you:

// required 'using' statements at the beginning of the file
using System.Linq;
using OutSystems.SOAP.API;
/* ... */
// replace the 'MssSetEncoding' function placeholder with the following code
public void MssSetEncoding(string ssEncoding) {
ISOAPClient client = SoapRequest.GetCurrentClient();
// Create a binding based on the one already set-up.
var customBinding = new CustomBinding(client.Endpoint.Binding);
// Get the current TextMessageEncodingBindingElement
// that will be replaced with the custom one defined in a helper class.
var textEncodingElement = customBinding.Elements.OfType<TextMessageEncodingBindingElement>().Single();
// Create a new instance of our custom TextMessageEncodingBindingElement
// with the correct encoding.
var customTextEncodingElement = new CustomTextMessageEncodingBindingElement(ssEncoding, client.Endpoint.Binding.MessageVersion);
// Insert customTextEncodingElement in the correct place.
// Required to respect the mandatory order of elements in a custom binding: https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/custom-bindings
customBinding.Elements.Remove(textEncodingElement);
customBinding.Elements.Insert(customBinding.Elements.Count - 1, customTextEncodingElement);
//Replace previous binding with the custom one
client.Endpoint.Binding = customBinding;
}

6. In Service Studio, add a reference to the "SetEncoding" action of your extension in your application module.

7. In the flow of the SOAP callback of your SOAP Web Service, i.e. the flow of "OnBeforeRequestAdvanced", drag the "SetEncoding" action to the flow and enter the desired encoding value in the "Encoding" input parameter.

8. Publish the application module and test the application, checking that the requests made to the consumed SOAP Web Service are done with the correct encoding and that no runtime errors occur due to encoding mismatch.