[英]Send binary message in MSMQ
In a .NET service, I'd like to send the content of a stream in a MSMQ message. 在.NET服务中,我想通过MSMQ消息发送流的内容。 I receive a Stream object from WCF (which can be an image, a .doc file or anything else) and I would just like to put the content in MSMQ.
我从WCF接收到一个Stream对象(可以是图像,.doc文件或其他任何东西),我只想将内容放入MSMQ中。 I tried setting the System.Messaging.Message.BodyStream property to the stream object, but I receive a "Method not supported" error when I try to Send the message.
我尝试将System.Messaging.Message.BodyStream属性设置为流对象,但是在尝试发送消息时收到“方法不支持”错误。
Does anyone have a sample or tutorial on how to do that ? 有没有人提供有关该操作的示例或教程?
I'm looking for something like this : 我正在寻找这样的东西:
Message contract : 留言合同:
<MessageContract()> _
Class UploadStreamMessage
<MessageHeader()> _
Public guidCaller As Guid
<MessageBodyMember()> _
Public data As Stream
End Class
And then the WriteToMSMQ method implementation : 然后执行WriteToMSMQ方法:
<OperationBehavior(TransactionScopeRequired:=True, Impersonation:=ImpersonationOption.Required)> _
Public Sub WriteToMSMQ (ByVal stream As IServiceMSMQ.UploadStreamMessage) Implements IServiceMSMQ.WriteToMSMQ
Using queue As New MessageQueue(NOM_QUEUE)
Dim msmqMessage As New Message
msmqMessage = New System.Messaging.Message()
msmqMessage.Body = stream.data
queue.Send(msmqMessage, transaction)
stream.data.Close()
End Using
Thanks ! 谢谢 !
Here's an example of an email queue I use... I think you need to add a formatter. 这是我使用的电子邮件队列的示例...我认为您需要添加一个格式化程序。
Here are the DTOs: 这是DTO:
[Serializable]
public class EmailOut
{
private readonly string _fromEmail;
private readonly string _toEmail;
private readonly string _subject;
private readonly string _body;
private readonly string _cc;
private readonly IList<EmailOutAttachment> _emailOutAttachments;
public EmailOut(string fromEmail, string toEmail, string subject, string body, string cc, IList<EmailOutAttachment> emailOutAttachments)
{
_fromEmail = fromEmail;
_cc = cc;
_emailOutAttachments = emailOutAttachments;
_toEmail = toEmail;
_subject = subject;
_body = body;
}
public string FromEmail
{
get { return _fromEmail; }
}
public string ToEmail
{
get { return _toEmail; }
}
public string Subject
{
get { return _subject; }
}
public string Body
{
get { return _body; }
}
public string Cc
{
get { return _cc; }
}
public IList<EmailOutAttachment> EmailOutAttachments
{
get { return _emailOutAttachments; }
}
public override string ToString()
{
return string.Format("FromEmail: {0}, ToEmail: {1}, Subject: {2}, Body: {3}, Cc: {4}", _fromEmail, _toEmail, _subject, _body, _cc);
}
}
[Serializable]
public class EmailOutAttachment
{
private readonly string _name;
private readonly byte[] _bytes;
private readonly int _size;
//Null ok for emailer
private readonly string _mimeEncodingType;
/// <summary>
/// Null ok for mime type.
/// </summary>
/// <param name="name"></param>
/// <param name="bytes"></param>
/// <param name="size"></param>
/// <param name="mimeEncodingType">Null ok for mime type.</param>
public EmailOutAttachment( string name, byte[] bytes, int size, string mimeEncodingType)
{
_bytes = bytes;
_name = name;
_size = size;
_mimeEncodingType = mimeEncodingType;
}
public byte[] Bytes
{
get { return _bytes; }
}
public int Size
{
get { return _size; }
}
public string MimeEncodingType
{
get { return _mimeEncodingType; }
}
public string Name
{
get { return _name; }
}
} }
Here is an extension you need. 这是您需要的扩展。
public static Byte[] GetBytes(this Stream input)
{
if (null == input || 0 == input.Length)
return new Byte[0];
byte[] bytes = new byte[input.Length];
int numBytesToRead = (int)input.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = input.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
return bytes;
}
This is the sending mechanism: 这是发送机制:
using (Message msg = new Message())
{
msg.Body = new EmailOut(from, to, title, body, cc, attachments);
msg.Recoverable = true;
msg.Formatter = new BinaryMessageFormatter();
string queuePath = config.MsmqEmailBox;
//if this queue doesn't exist we will create it
//if (!MessageQueue.Exists(queuePath))
// MessageQueue.Create(queuePath);
using (MessageQueue messageQueue = new MessageQueue(queuePath))
{
messageQueue.Formatter = new BinaryMessageFormatter();
messageQueue.Send(msg);
}
}
This is the receiving end of it... 这是它的接收端...
using (var messageQueue = new MessageQueue(_config.MsmqEmailBox))
try
{
_log.Debug("Retrieving queue");
messageQueue.Formatter = new BinaryMessageFormatter();
messageQueue.MessageReadPropertyFilter.SetAll();
List<Message> allMessages = messageQueue.GetAllMessages().ToList();
if(0 < allMessages.Count)
SendEmailNoMsmq.Instance.SendDotNetEmail(_config.DevRecipient,string.Format("Sending queued messages, Count: {0}, Time: {1}",allMessages.Count,DateTime.Now),"MSMQ MAIL SEND", _config, _log);
foreach (Message message in allMessages)
{
EmailOut msg = (EmailOut)message.Body;
_log.Info(string.Format("Sending email id:{0} to:{1} ", message.Id, msg.ToEmail));
SendEmailNoMsmq.Instance.SendDotNetEmail(msg, _config, _log);
messageQueue.ReceiveById(message.Id);
}
}
catch (Exception ex)
{
_log.Error("Error ex:" + ex);
throw;
}
}
MSMQ doesn't support streaming, because it is explicitly message based and loosely coupled. MSMQ不支持流传输,因为它显式基于消息且松散耦合。 You should declare field of contract as
byte[]
and gather data there. 您应该将合同字段声明为
byte[]
并在那里收集数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.