本文整理汇总了C#中System.Messaging.MessageQueueTransaction.Begin方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueueTransaction.Begin方法的具体用法?C# MessageQueueTransaction.Begin怎么用?C# MessageQueueTransaction.Begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueueTransaction
的用法示例。
在下文中一共展示了MessageQueueTransaction.Begin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnAdd_Click
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
string queueName = ConfigurationManager.AppSettings["MSMQLocation"];
MessageQueue rmTxnQ = new MessageQueue(queueName);
rmTxnQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(ProcessMessage) });
foreach (ListBoxItem itm in files.Items)
{
MessageQueueTransaction msgTx = new MessageQueueTransaction();
msgTx.Begin();
try
{
string argument = "-i \"{0}\" -o \"{1}\" --preset \"" + ConfigurationManager.AppSettings["HandbrakePreset"] + "\"";
string destination = txtDestination.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(itm.ToolTip.ToString()) + ".m4v";
ProcessMessage p = new ProcessMessage() { CommandLine = argument, DestinationURL = destination, OrignalFileURL = itm.ToolTip.ToString() };
rmTxnQ.Send(p, msgTx);
results.Items.Insert(0, string.Format("{0} added to queue", p.OrignalFileURL));
msgTx.Commit();
}
catch (Exception ex)
{
results.Items.Insert(0, ex.Message);
msgTx.Abort();
}
}
}
示例2: Run
void Run()
{
MessageQueueTransaction transaction = new MessageQueueTransaction();
while (!shouldStop)
{
try
{
transaction.Begin();
Message message = requestQueue.Receive(new TimeSpan(0, 0, 1), transaction);
responseQueue.Send(message, transaction);
transaction.Commit();
synchronizedInvoke.BeginInvoke(addMessage, new object[] { requestQueueName, message });
}
catch (MessageQueueException e)
{
if (e.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
Console.WriteLine("Receive failure on " + requestQueueName + " " + e.StackTrace,
System.Diagnostics.EventLogEntryType.Error);
}
finally
{
if (transaction.Status == MessageQueueTransactionStatus.Pending)
{
transaction.Abort();
}
}
}
}
示例3: Run
static void Run()
{
MessageQueue queue = null;
MessageQueueTransaction trans = null;
try
{
queue = new MessageQueue();
queue.Path = Constants.QUEUE_PATH;
queue.DefaultPropertiesToSend.Recoverable = true;
trans = new MessageQueueTransaction();
trans.Begin();
MyOrder order = new MyOrder();
order.ID = DateTime.Now.Ticks.ToString();
order.Name = "Order_" + order.ID;
Message msg = new Message(order);
queue.Send(msg, trans);
trans.Commit();
}
catch (Exception ex)
{
trans.Abort();
}
finally
{
queue.Close();
}
Console.WriteLine("message sent..");
}
示例4: Main
private static void Main(string[] args)
{
const string message = "Testing MSMQ";
List<MessageQueue> queues = CreateQueues(".\\private$\\FirstQueue",
".\\private$\\SecondQueue",
".\\private$\\ThirdQueue");
var sender = new Sender();
var receiver = new Receiver();
/*
* Send to all the queues within a transaction
*/
using (var transaction = new MessageQueueTransaction())
{
transaction.Begin();
foreach (MessageQueue queue in queues)
{
sender.Send(queue, message, transaction);
}
transaction.Commit();
}
/*
* Read from all the queues
*/
receiver.StartReading(queues);
Console.ReadKey();
}
示例5: GetMessageFromQueue
public override void GetMessageFromQueue(string messageId, MessageQueue queue, Action<Message> processMethod)
{
using (var transaction = new MessageQueueTransaction())
{
transaction.Begin();
Logger.DebugFormat("Transaction for {0} started.", messageId);
Message message;
try
{
message = queue.ReceiveById(messageId, TimeSpan.FromSeconds(30), transaction);
Logger.DebugFormat("Message with id {0} received.", messageId);
}
catch (Exception ex)
{
transaction.Abort();
Logger.Error(
string.Concat("Failed to receive message with id ", messageId, "transactions aborted.")
, ex);
return;
}
if (message != null)
{
processMethod(message);
}
if (transaction.Status != MessageQueueTransactionStatus.Aborted)
transaction.Commit();
}
}
示例6: OnPeekCompleted
private void OnPeekCompleted(object sender, PeekCompletedEventArgs e)
{
if (OnMessageReceived == null) return;
var transaction = new MessageQueueTransaction();
transaction.Begin();
try
{
var queueMessage = inputQueue.Receive(transaction);
var headers = GetMessageHeaders(queueMessage);
var message = new ChannelMessage(queueMessage.BodyStream);
message.AddHeader(headers);
OnMessageReceived(this, new MessageReceivedEventArgs { Message = message });
transaction.Commit();
}
catch (Exception)
{
transaction.Abort();
throw;
}
finally
{
inputQueue.BeginPeek();
}
}
示例7: Send
public void Send(Object message)
{
// Open the queue.
using (var queue = new MessageQueue(_queueName.GetQueueFormatName()))
{
// Set the formatter to JSON.
queue.Formatter = new MsmqMessageFormatter();
// Since we're using a transactional queue, make a transaction.
using (MessageQueueTransaction mqt = new MessageQueueTransaction())
{
mqt.Begin();
// Create a simple text message.
Message myMessage = new Message(message, new MsmqMessageFormatter());
myMessage.Label = message.GetType().FullName;
myMessage.ResponseQueue = new MessageQueue(_queueName.GetQueueFormatName());
// Send the message.
queue.Send(myMessage, mqt);
mqt.Commit();
}
}
}
示例8: Send2WithLabelWithTransaction
public void Send2WithLabelWithTransaction ()
{
String label1 = "label1";
String label2 = "label2";
Message sent1 = new Message ("Message 1", new BinaryMessageFormatter ());
Message sent2 = new Message ("Message 2", new BinaryMessageFormatter ());
MessageQueue mq = MQUtil.GetQueue (MQUtil.CreateQueueName (), true);
mq.MessageReadPropertyFilter.SetAll ();
Assert.IsTrue(mq.Transactional, "Message Queue should be transactional");
using (MessageQueueTransaction tx = new MessageQueueTransaction ()) {
tx.Begin ();
mq.Send (sent1, label1, tx);
mq.Send (sent2, label2, tx);
tx.Commit ();
Message received1 = mq.Receive ();
Assert.IsNotNull (received1.TransactionId, "TransactionId not set");
Message received2 = mq.Receive ();
Assert.IsNotNull (received2.TransactionId, "TransactionId not set");
Assert.AreEqual (received1.TransactionId, received2.TransactionId, "Messages have differing TransactionIds");
Assert.IsTrue (received1.TransactionId.Length > 1);
Assert.AreEqual (sent1.Body, received1.Body, "Message 1 not delivered correctly");
Assert.AreEqual (sent2.Body, received2.Body, "Message 2 not delivered correctly");
Assert.AreEqual (label1, received1.Label, "Label 1 not passed correctly");
Assert.AreEqual (label2, received2.Label, "Label 2 not passed correctly");
}
}
示例9: MoveMessage
static void MoveMessage(Message message, string destinationQueueName, bool leaveCopyInSourceQueue)
{
var sourceQueuePath = message.QueuePath;
var destinationQueuePath = MsmqUtil.GetFullPath(destinationQueueName);
using (var transaction = new MessageQueueTransaction())
{
transaction.Begin();
try
{
var sourceQueue = new MessageQueue(sourceQueuePath) {MessageReadPropertyFilter = DefaultFilter()};
var destinationQueue = new MessageQueue(destinationQueuePath);
var msmqMessage = sourceQueue.ReceiveById(message.Id, transaction);
destinationQueue.Send(msmqMessage, transaction);
if (leaveCopyInSourceQueue)
{
sourceQueue.Send(msmqMessage, transaction);
}
transaction.Commit();
}
catch
{
transaction.Abort();
throw;
}
}
Messenger.Default.Send(new MessageMoved(message, sourceQueuePath, destinationQueuePath, leaveCopyInSourceQueue));
}
示例10: MangledMessageIsNotReceived
public void MangledMessageIsNotReceived()
{
using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
{
var transaction = new MessageQueueTransaction();
transaction.Begin();
messageQueue.Send(new Message
{
Extension = Encoding.UTF32.GetBytes("this is definitely not valid UTF8-encoded JSON")
}, transaction);
transaction.Commit();
}
Thread.Sleep(5000);
CleanUpDisposables();
using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
{
messageQueue.MessageReadPropertyFilter = new MessagePropertyFilter
{
Extension = true
};
var transaction = new MessageQueueTransaction();
transaction.Begin();
var message = messageQueue.Receive(transaction);
Assert.That(message, Is.Not.Null);
Assert.That(Encoding.UTF32.GetString(message.Extension), Is.EqualTo("this is definitely not valid UTF8-encoded JSON"));
transaction.Commit();
}
}
示例11: _orderQueue_PeekCompleted
static void _orderQueue_PeekCompleted(object sender, PeekCompletedEventArgs e)
{
var orderQueue = (MessageQueue)sender;
var transaction = new MessageQueueTransaction();
transaction.Begin();
Order order;
try
{
var receivedMessage = orderQueue.Receive(transaction);
receivedMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
order = Order.DeserializeXml(receivedMessage.Body.ToString());
using (var db = new OrderContext())
{
db.Orders.Add(order);
db.SaveChanges();
}
transaction.Commit();
}
catch
{
// TODO: Log something
transaction.Abort();
}
finally
{
orderQueue.BeginPeek();
}
}
示例12: sendDeclMessage
public Boolean sendDeclMessage(string messageId, string outId, int sendType, string messageDetail, int place)
{
if (messageId == null && messageId == ""
&& outId == null && outId == ""
&& messageDetail == null && messageDetail == ""
&& sendType == 0
&& place == 0)
{
return false;
}
MessageQueueTransaction msgTransaction = new MessageQueueTransaction();
try
{
mq = mqFactory.CreateMsgQueueFactory(sendType, place);
using (TransactionScope scope = new TransactionScope())
{
msgTransaction.Begin();
XmlDocument xmldoc = new XmlDocument();
if (place == (int)SendPlace.GOVERNMENT)
{
String key = configUtil.getGovPwd();
messageDetail = AESUtil.AesEncoding(messageDetail, key, Encoding.UTF8);
mq.SendEncryptMessage(messageDetail, msgTransaction, messageId);
}
else
{
xmldoc.LoadXml(messageDetail);
mq.SendMessage(xmldoc, msgTransaction, messageId);
}
Logger.Debug(messageDetail);
messageDeclService.createDeclMessage(messageId, outId, sendType, messageDetail,place);
messageRespService.createResponseMessage(messageId, outId, sendType,place);
scope.Complete();
msgTransaction.Commit();
return true;
}
} catch (Exception e)
{
msgTransaction.Abort();
if ((int)CustomsDeclarationProxy.Constant.CustomsMessageType.MANIFEST== sendType)
{
Logger.Error("shipmentPackId:" + outId + "send and insert manifest message failed!", e);
}
else if ((int)CustomsDeclarationProxy.Constant.CustomsMessageType.ORDER == sendType)
{
Logger.Error("shipmentPackId:" + outId + "send and insert order message failed!", e);
}
return false;
}
}
示例13: btnR_Click
private void btnR_Click(object sender, EventArgs e)
{
try
{
if (this.chbChoose1.Checked)
{
MessageQueue queue = new MessageQueue(this.txtMSMQ1.Text) {
Formatter = new BinaryMessageFormatter()
};
System.Messaging.Message message = new System.Messaging.Message();
MessageQueueTransaction transaction = new MessageQueueTransaction();
transaction.Begin();
try
{
message = queue.Receive(TimeSpan.FromMilliseconds(500.0), transaction);
}
catch
{
transaction.Commit();
}
StreamReader reader = new StreamReader(message.BodyStream, Encoding.ASCII);
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtPath1.Text = this.saveFileDialog1.FileName;
FileStream output = new FileStream(this.txtPath1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter writer = new BinaryWriter(output);
writer.Write(Encoding.Default.GetBytes(reader.ReadToEnd()));
writer.Flush();
writer.Close();
transaction.Commit();
MessageBox.Show("发送成功!");
}
}
else
{
MessageQueue queue2 = new MessageQueue(this.txtMSMQ1.Text) {
Formatter = new BinaryMessageFormatter()
};
System.Messaging.Message message2 = new System.Messaging.Message();
StreamReader reader2 = new StreamReader(queue2.Receive(TimeSpan.FromMilliseconds(500.0)).BodyStream, Encoding.ASCII);
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtPath1.Text = this.saveFileDialog1.FileName;
FileStream stream2 = new FileStream(this.txtPath1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter writer2 = new BinaryWriter(stream2);
writer2.Write(Encoding.Default.GetBytes(reader2.ReadToEnd()));
writer2.Flush();
writer2.Close();
MessageBox.Show("发送成功!");
}
}
}
catch (Exception exception)
{
MessageBox.Show("发送失败!原因:" + exception.Message);
}
}
示例14: Enqueue
public void Enqueue(IDbConnection connection, string queue, string jobId)
{
using (var messageQueue = GetMessageQueue(queue))
using (var message = new Message { Label = jobId })
using (var transaction = new MessageQueueTransaction())
{
transaction.Begin();
messageQueue.Send(message, transaction);
transaction.Commit();
}
}
示例15: EnqueueJobId
public static void EnqueueJobId(string queue, string jobId)
{
using (var messageQueue = CleanMsmqQueueAttribute.GetMessageQueue(queue))
using (var message = new Message { Body = jobId, Label = jobId, Formatter = new BinaryMessageFormatter() })
using (var transaction = new MessageQueueTransaction())
{
transaction.Begin();
messageQueue.Send(message, transaction);
transaction.Commit();
}
}