I may be a little late, but a hoe it helps a little.
I recently had a similar goal (also related to biztalk) where I needed to change the url based on some value sent by message. I tried using the ApplyDispatchBehavior method, but it was never called, and also, I could not see how to access the message from here, so I started looking for the BeforeSendRequest method (in the Inspector class).
Here is what I came up with:
object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel) { var queryDictionary = HttpUtility.ParseQueryString(request.Headers.To.Query); string parameterValue = queryDictionary[this.BehaviourConfiguration.QueryParameter]; //Only change parameter value if it exists if (parameterValue != null) { MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue); request = buffer.CreateMessage(); //Necessary in order to read the message without having WCF throwing and error saying //the messas was already read var reqAux = buffer.CreateMessage(); //For some reason the message comes in binary inside tags <Binary>MESSAGE</Binary> using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(reqAux.ToString().Replace("<Binary>", "").Replace("</Binary>", "")))) { ms.Position = 0; string val = ExtractNodeValueByXPath(ms, this.BehaviourConfiguration.FieldXpath); queryDictionary.Set(this.BehaviourConfiguration.QueryParameter, DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + this.BehaviourConfiguration.Message + (string.IsNullOrWhiteSpace(val) ? string.Empty : "_" + val) + ".xml"); UriBuilder ub = new UriBuilder(request.Headers.To); ub.Query = queryDictionary.ToString(); request.Headers.To = ub.Uri; } } return null; }
So, I found that messing around with request.Headers.To , I could change the endpoint.
I had several problems getting the contents of the message and most of the examples on the Internet (showing that using MessageBuffer.CreateNavigator or Message.GetBody <string>, which always threw an exploit that I could not get around) would not give me a biztalk message, but rather soap message? ... not sure, but he had a headline
The body and inside the body had some base64 string, which was not my biztalk message.
Also, as you can see in Convert.FromBase64String(reqAux.ToString().Replace("<Binary>", "").Replace("</Binary>", "")) , I had to make it ugly substitution. I donβt do why this happens in base64, maybe in some WCF configuration ?, but by doing this, I could find my value.
NOTE. I have not fully tested this, but so far this has worked for my examples.
By the way, any idea that I can switch MemoryStream with this way will become a more streaming solution?