Earlier I reported that we encountered an issue with communication via IBM MQ WCF Channel over a clustered IBM WebSphere MQ-queue. The problem was already known within IBM and the fixed software ready on the shelves, and naturally we were very willing to be a first beta-tester. With this IBM fix, the EndPointNotFound problem was indeed solved. Sadly, we next ran into another issue. This time it manifested itself in our own service implementation at the receiving side. Upon receiving a MQ-request, the service responded with a technical error. Due the holiday season it took some elapse time before the right people were available and able to investigate the cause. The problem analyse led to the suspicion that the presence of xml-namespaces in the webrequest was unexpected, and therefore the service refused the request:
<ServiceRequestName xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> ... </ServiceRequestName>
<ServiceRequestName xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> ... </ServiceRequestName>
Both namespaces are default included when serializing a System.ServiceModel.Channels.Message instance from inside WCF client processing. You can however prevent this by implementing an subclass of System.ServiceModel.Channel.Body.Writer, and override the OnWriteBodyContents method. Next inject this method within the WCF Channel:
System.ServiceModel.Channels.Message message =
Message.CreateMessage(
MessageVersion.None,
string.Empty,
new XmlBodyWriter<TRrequest>(request));
var responseMessage = webmqclient.Request(message);
...
public class XmlBodyWriter<TBody> : BodyWriter
{
private readonly TBody request;
public XmlBodyWriter(TBody request) : base(true)
{
this.request = request;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
WriteBodyContents(SerializeRequest(), writer);
}
private static void WriteBodyContents(StringBuilder output, XmlWriter writer)
{
using (var reader = new StringReader(output.ToString()))
{
using (XmlReader xmlReader = XmlReader.Create(reader))
{
writer.WriteNode(xmlReader, true);
}
}
}
private StringBuilder SerializeRequest()
{
var output = new StringBuilder();
using (var xmlWriter = XmlWriter.Create(output,
new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }))
{
var serializer = new XmlSerializer(typeof(TBody));
// Service does not expect xml-namespacing; including the standard
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xmlns:xsd="http://www.w3.org/2001/XMLSchema"
// Omit these from the serialized request
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(xmlWriter, request, ns);
}
return output;
}
}
System.ServiceModel.Channels.Message message =
Message.CreateMessage(
MessageVersion.None,
string.Empty,
new XmlBodyWriter<TRrequest>(request));
var responseMessage = webmqclient.Request(message);
...
public class XmlBodyWriter<TBody> : BodyWriter
{
private readonly TBody request;
public XmlBodyWriter(TBody request) : base(true)
{
this.request = request;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
WriteBodyContents(SerializeRequest(), writer);
}
private static void WriteBodyContents(StringBuilder output, XmlWriter writer)
{
using (var reader = new StringReader(output.ToString()))
{
using (XmlReader xmlReader = XmlReader.Create(reader))
{
writer.WriteNode(xmlReader, true);
}
}
}
private StringBuilder SerializeRequest()
{
var output = new StringBuilder();
using (var xmlWriter = XmlWriter.Create(output,
new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }))
{
var serializer = new XmlSerializer(typeof(TBody));
// Service does not expect xml-namespacing; including the standard
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xmlns:xsd="http://www.w3.org/2001/XMLSchema"
// Omit these from the serialized request
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(xmlWriter, request, ns);
}
return output;
}
}
With this modification to the serialization processing of the WCF MQ request, the service at the receiving WebSphere MQ side now replies with a correct functional response.
No comments:
Post a Comment