To send the SOAP headers required by the Royal Mail Shipping API we will use the functionality available in WCF to set headers and intercept and alter messages as they are being sent and received by the client.
We have already built a ClientMessageInspector class (see Part 1 and 2) implementing the IClientMessageInspector interface. Now we need to add this message inspector to the client’s message inspector collection.
Create a new class implementing IEndpointBehavior, add the method stubs need to implement the interface. In the ApplyClientBehaviour method we will hook up the message inspector, so that this will be used for each message that is sent or received.
We also want to pass credentials to the ClientMessageInspector, so create a public method that will set a credential variable (taking the client id and secret as string). Then call the SetCredentials method on the Message Inspector before adding it to the Message Inspector collection.
The Shipping API also requires a set of SOAP and WSS namespaces to be added to the message sent from the client.
This is very straight forward to do creating a custom Message class and overriding the various OnWrite methods.
To use our custom message with outgoing requests we implement the IClientMessageFormatter interface and add our Message class in the SerializeRequest method.
This means that our custom message will be used every time a message needs to be serialized before being sent to the remote endpoint.
In turn our Message Formatter needs to be set as the proxy message formatter, we do this by utilising the IOperationBehaviour interface that has a set of methods like IEndpointBehaviour. The two interfaces differ in the scope that they effect.
We use the ApplyClientBehaviour method to add our message formatter.
We now have everything set up in the WCF part of our integration. We are modifiying the HTTP headers to send security information with our request. We have modified the namespaces and added SOAP elements to the message.
In the next Post
We will look at how to call our client proxy from X++ code.