c# - MSMQ为什么不发送空格字符?

标签 c# xml-serialization character-encoding msmq tostring

我正在探索MSMQ服务,并且编写了一个简单的控制台客户端-服务器应用程序,它将客户端的每个击键发送到服务器。可以理解,只要碰到控制字符(DELESCINS等),服务器就会引发错误。但是,每当我键入一个空格字符时,服务器都会收到该数据包,但不会引发错误,也不会显示空格。

服务器:

namespace QIM
{
    class Program
    {
        const string QUEUE = @".\Private$\qim";
        static MessageQueue _mq;
        static readonly object _mqLock = new object();
        static XmlSerializer xs;

        static void Main(string[] args)
        {
            lock (_mqLock)
            {
                if (!MessageQueue.Exists(QUEUE))
                    _mq = MessageQueue.Create(QUEUE);
                else
                    _mq = new MessageQueue(QUEUE);
            }
            xs = new XmlSerializer(typeof(string));
            _mq.BeginReceive(new TimeSpan(0, 1, 0), new object(), OnReceive);
            while (Console.ReadKey().Key != ConsoleKey.Escape) { }
        }

        static void OnReceive(IAsyncResult result)
        {
            Message msg;
            lock (_mqLock)
            {
                try
                {
                    msg = _mq.EndReceive(result);
                    Console.Write(".");
                    Console.Write(xs.Deserialize(msg.BodyStream));
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
            _mq.BeginReceive(new TimeSpan(0, 1, 0), new object(), OnReceive);
        }
    }
}


客户:

namespace QIM_Client
{
    class Program
    {
        const string QUEUE = @".\Private$\qim";
        static MessageQueue _mq;

        static void Main(string[] args)
        {
            if (!MessageQueue.Exists(QUEUE))
                _mq = MessageQueue.Create(QUEUE);
            else
                _mq = new MessageQueue(QUEUE);
            ConsoleKeyInfo key = new ConsoleKeyInfo();
            while (key.Key != ConsoleKey.Escape)
            {
                key = Console.ReadKey();
                _mq.Send(key.KeyChar.ToString());
            }
        }
    }
}


客户输入:


  测试中,测试中...


服务器输出:


  T.e.t.i.n.g。,.. T.e.s.t.i.n.g ......


您会注意到空格字符会发送一条消息,但不会显示该字符。

最佳答案

您的问题与MSMQ无关,而与XmlSerializer类有关。看到:

var key = Console.ReadKey();

XmlSerializer s = new XmlSerializer(typeof(string));

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    s.Serialize(ms, key.KeyChar.ToString());

    ms.Position = 0;

    var foo = (string)s.Deserialize(ms);
}


如果在控制台中键入空格,则会看到key.KeyChar.ToString()产生" ",但是foo等于""。由于XmlReader的默认实现,因此XmlSerializer类将仅空白字符串视为空;如果字符串包含任何其他字符,则保留前导空格和尾随空格。空格确实会序列化,但是反序列化会将其变成空字符串。

使用此代替:

Console.Write(
    s.Deserialize(System.Xml.XmlReader.Create(msg.BodyStream, 
                                          new System.Xml.XmlReaderSettings() 
                                          { 
                                              IgnoreWhitespace = false 
                                          })));

关于c# - MSMQ为什么不发送空格字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2899321/

相关文章:

c# - 如何限制通用扩展的类型

c# - 在 Silverlight 中将字节数组转换为字符串?

c# - XML 序列化和 DefaultValue ("") c# 中的相关问题

c# - XML 序列化 : 64Bit Windows7, oct 处理器核心,32GB RAM 和 OutOfMemory 异常

c# - 如何获取 Array.IndexOf<string>(string[], string) 方法信息?

c# - 无法识别方法 'System.String ToString()' 方法,并且无法将此方法翻译成存储表达式

c# - 如何序列化到日期时间

utf-8 - Graphviz 不支持 UTF-8 编码

python - Unicode解码错误: 'utf-8' can't decode byte

PHP PDO setAttribute 和错误处理