我将WebSphere MQ v7服务器和WebSphere MQ Client v7.5与amqmdnet.dll 7.5.0.0一起用作.NET库,以便通过WebSphere MQ进行通信。有时,当我尝试从队列中读取MQMessage
时,我会MQException
MQRC_RFH_FORMAT_ERROR
原因。
var message = new MQMessage();
queue.Get(message);
此行为似乎取决于邮件的发件人及其内容。它通常不适用于使用float或double属性发送消息的不同平台上的系统。
答案 0 :(得分:3)
这是IBM的amqmdnet.dll中的全球化错误。在客户端(strmqtrc.exe
)上启用MQ跟踪后,我尝试再次收到消息,我在其中一个跟踪文件中找到了这个消息:
00001F21 11:48:00.351013 7104.1 : Exception received
System.FormatException
Message: Input string was not in a correct format.
StackTrace:
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToSingle(String value)
at IBM.WMQ.MQMarshalMessageForGet.GetValueAsObject(String dt, String propValue)
at IBM.WMQ.MQMarshalMessageForGet.ProcessAllAvailableRFHs()
00001F22 11:48:00.351115 7104.1 : We are not sucessful in parsing one of theRFH2Header.Raise the RFH_FORMAT exception and breakfurther processing in loop
00001F23 11:48:00.351825 7104.1 : MQException CompCode: 2 Reason: 2421
使用.NET Reflector反复编译MQMarshalMessageForGet
显示了这一点:
private object GetValueAsObject(string dt, string propValue)
{
...
switch (dt)
{
...
case "r4":
return Convert.ToSingle(propValue);
case "r8":
return Convert.ToDouble(propValue);
...
}
...
}
在当前系统的文化中读取消息而不是在传输预期文化中(在所有平台上都应该相同)!简单的测试来重现这个问题:
[Test]
public void PutAndGetMessageWithFloatProperty() {
using (MQQueue queue = _queueManager.AccessQueue(TestQueue, MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_AS_Q_DEF))
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
MQMessage message = new MQMessage();
message.SetFloatProperty("TEST_SINGLE", 14.879f);
message.WriteString("some string");
message.Format = MQC.MQFMT_STRING;
queue.Put(message); // Writes property value as 14.879
Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ");
MQMessage readMessage = new MQMessage();
queue.Get(readMessage); // Throws MQException because 14,879 is correct format
queue.Close();
}
}
解决方法强>
我正在使用这个简单的范围类:
public class CultureForThreadScope : IDisposable {
private readonly CultureInfo oldCulture;
public CultureForThreadScope(CultureInfo culture) {
oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
}
public void Dispose() {
Thread.CurrentThread.CurrentCulture = oldCulture;
}
}
我正在将每个Get
调用包装到范围内。
using (new CultureForThreadScope(CultureInfo.InvariantCulture)) {
destination.Get(message, getOptions);
}