我想使MSMQ消息,這將有文字例如MSMQ自定義消息格式
<order><data id="5" color="blue"/></order>
這是標準的XML。到目前爲止,我已經使Serializable類
[Serializable]
public class order
string id
string color
我使用BinaryFormatter。當我檢查message.BodyStream有一些不應該在那裏的字符(00,01,FF),那麼我無法接收這條消息沒有錯誤。
這個任務看似簡單,只是把文字
<order><data id="5" color="blue"/></order>
到MSMQ。
礦整個重要的代碼:
public static void Send()
{
using (message = new Message())
{
request req = new request("1", "blue");
message.Recoverable = true;
message.Body = req.ToString();
message.Formatter = new BinaryMessageFormatter();
using (msmq = new MessageQueue(@".\Private$\testrfid"))
{
msmq.Formatter = new BinaryMessageFormatter();
msmq.Send(message, MessageQueueTransactionType.None);
}
}
}
[Serializable]
public class request
{
private readonly string _order;
private readonly string _color;
public request(string order, string color)
{
_order = order;
_color = color;
}
public request()
{ }
public string Order
{
get { return _order; }
}
public string Color
{
get { return _color; }
}
public override string ToString()
{
return string.Format(@"<request> <job order = ""{0}"" color = ""{1}"" /> </request>",_order,_color);
}
}
Downvoter:這是不禮貌的反對票沒有留下評論。 –
事實上,我編輯過這個問題。原來的形式非常模糊,所以可能多數民衆贊成在爲什麼。 –
當使用'BinaryMessageFormatter'時,你不應該關心它看起來像什麼。雖然您可能試圖將其序列化爲XML,但「BinaryMessageFormatter」沒有做出這樣的保證。重要的是,您使用相同的格式化程序來讀取消息,就像您寫消息一樣。使用該格式化程序時,它的意思不是人類可讀的。 – tcarvin