本文整理汇总了C#中System.Messaging.MessageQueueTransaction.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueueTransaction.Dispose方法的具体用法?C# MessageQueueTransaction.Dispose怎么用?C# MessageQueueTransaction.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueueTransaction
的用法示例。
在下文中一共展示了MessageQueueTransaction.Dispose方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dequeue
public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
{
string jobId = null;
MessageQueueTransaction transaction;
var queueIndex = 0;
do
{
cancellationToken.ThrowIfCancellationRequested();
queueIndex = (queueIndex + 1) % queues.Length;
var queueName = queues[queueIndex];
transaction = new MessageQueueTransaction();
using (var messageQueue = GetMessageQueue(queueName))
{
try
{
transaction.Begin();
var message = queueIndex == 0
? messageQueue.Receive(SyncReceiveTimeout, transaction)
: messageQueue.Receive(new TimeSpan(1), transaction);
message.Formatter = _formatter.Value;
jobId = (string)message.Body;
}
catch (MessageQueueException ex)
{
transaction.Abort();
transaction.Dispose();
if (ex.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
{
throw;
}
}
}
} while (jobId == null);
return new MsmqFetchedJob(transaction, jobId);
}
示例2: Main
static void Main(string[] args)
{
string path = @"kim-msi\private$\kimqueue2";
MessageQueue queue = null;
if (!MessageQueue.Exists(path))
{
queue = MessageQueue.Create(path, true);
}
else
queue = new MessageQueue(path);
//queue.Purge();
//return;
// queue.ReceiveCompleted += Queue_ReceiveCompleted;//非同步寫法BeginReceive接收Send事件
// queue.BeginReceive();
var messages = queue.GetAllMessages();
for (int i = 0; i < 10; i++)
{
if (messages.Count() == 0)
{
Message message = new Message(i.ToString());
queue.Send(message, MessageQueueTransactionType.Single);
}
else
{
MessageQueueTransaction tran = new MessageQueueTransaction();
tran.Begin();
var msg = queue.Receive(tran);
if (msg == null)
break;
System.Messaging.XmlMessageFormatter stringFormatter;
stringFormatter = new System.Messaging.XmlMessageFormatter(
new string[] { "System.String" });
msg.Formatter = stringFormatter;
Console.WriteLine(" e.Message:" + msg.Body);
if (i % 2 == 0)
tran.Dispose();
else
tran.Commit();
}
}
Console.ReadLine();
}
示例3: Send
public void Send(object message)
{
var transaction = new MessageQueueTransaction();
try
{
transaction.Begin();
_messageQueue.Send(message, transaction);
transaction.Commit();
}
catch
{
transaction.Abort();
throw;
}
finally
{
transaction.Dispose();
}
}
示例4: OnMessagePeeked
private void OnMessagePeeked(object sender, PeekCompletedEventArgs e)
{
var transaction = new MessageQueueTransaction();
try
{
transaction.Begin();
var message = _messageQueue.Receive(transaction);
MessageReceived(this, new MessageReceivedEventArgs(message));
}
catch
{
transaction.Abort();
throw;
}
finally
{
transaction.Dispose();
}
}
示例5: Receive
/// <summary>
/// Received the next available transport message from the input queue via MSMQ. Will create a new <see cref="MessageQueueTransaction"/> and stash
/// it under the <see cref="CurrentTransactionKey"/> key in the given <paramref name="context"/>. If one already exists, an exception will be thrown
/// (because we should never have to receive multiple messages in the same transaction)
/// </summary>
public async Task<TransportMessage> Receive(ITransactionContext context)
{
if (context == null) throw new ArgumentNullException("context");
if (_inputQueueName == null)
{
throw new InvalidOperationException("This MSMQ transport does not have an input queue, hence it is not possible to reveive anything");
}
var queue = GetInputQueue();
if (context.Items.ContainsKey(CurrentTransactionKey))
{
throw new InvalidOperationException("Tried to receive with an already existing MSMQ queue transaction - while that is possible, it's an indication that something is wrong!");
}
var messageQueueTransaction = new MessageQueueTransaction();
messageQueueTransaction.Begin();
context.OnCommitted(async () => messageQueueTransaction.Commit());
context.OnDisposed(() => messageQueueTransaction.Dispose());
context.Items[CurrentTransactionKey] = messageQueueTransaction;
try
{
var message = queue.Receive(TimeSpan.FromSeconds(1), messageQueueTransaction);
if (message == null)
{
messageQueueTransaction.Abort();
return null;
}
var headers = _extensionSerializer.Deserialize(message.Extension, message.Id);
var body = new byte[message.BodyStream.Length];
await message.BodyStream.ReadAsync(body, 0, body.Length);
return new TransportMessage(headers, body);
}
catch (MessageQueueException exception)
{
if (exception.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
{
return null;
}
if (exception.MessageQueueErrorCode == MessageQueueErrorCode.InvalidHandle)
{
_log.Info("Queue handle for '{0}' was invalid - will try to reinitialize the queue", _inputQueueName);
ReinitializeInputQueue();
}
if (exception.MessageQueueErrorCode == MessageQueueErrorCode.QueueDeleted)
{
_log.Warn("Queue '{0}' was deleted - will not receive any more messages", _inputQueueName);
return null;
}
throw new IOException(
string.Format("Could not receive next message from MSMQ queue '{0}'", _inputQueueName),
exception);
}
}
示例6: RunFrameworkJob
/// <summary>
/// Runs a single job thread
/// </summary>
/// <param name="data"></param>
private void RunFrameworkJob(Message queueMessage, MessageQueueTransaction transaction)
{
JobTimer jobTimer = new JobTimer(processorJob.MaxRunTimeMilliseconds, Thread.CurrentThread); // The timer terminates jobs that take too much time to run
try
{
int retryJobTimes = 0;
IWorkflowMessage workflowMessage = queueMessage.Body as IWorkflowMessage;
bool isCheckDepends = true;
if (workflowMessage.JobID < 0) // This is a re-run
{
workflowMessage.JobID = -workflowMessage.JobID;
isCheckDepends = false;
}
executor.RunFrameworkJob(workflowMessage, retryJobTimes, isCheckDepends);
retryJobTimes = 0;
}
catch (Exception e)
{
new WorkflowException("Error running framework job", e);
}
finally
{
pool.Release();
jobTimer.StopTimer();
Interlocked.Decrement(ref JobsRunning);
// Commit removing of the queue message at the end of the job
try
{
if (transaction != null)
{
transaction.Commit();
transaction.Dispose();
}
}
catch (InvalidOperationException e)
{
new WorkflowException("Error commiting queue transaction: The transaction you are trying to commit has not started.", e);
}
catch (MessageQueueException e)
{
new WorkflowException("Error commiting queue transaction: An internal Message Queuing error occured.", e);
}
}
//Find out the active Queue for the next Job
LoadActiveQueue();
}
示例7: SendMessage
/// <summary>
/// Sends a message to a message queue.
/// </summary>
/// <param name="target"></param>
/// <param name="message"></param>
private void SendMessage(MessageQueue target, Message message)
{
if (target.Transactional && target.Path.IndexOf("\\private$\\") != -1)
{
MessageQueueTransaction tran = new MessageQueueTransaction();
try
{
tran.Begin();
target.Send(message, tran);
tran.Commit();
}
catch
{
tran.Abort();
}
finally
{
tran.Dispose();
}
}
else
target.Send(message);
}
示例8: KillRemoteProcessMSG
public void KillRemoteProcessMSG(string computerName, string pcID)
{
var path = "FormatName:Direct=OS:" + computerName.ToLower() + "\\private$\\requestdeletequeue";
MessageQueue rmTmsq = new MessageQueue(path);
System.Messaging.Message msg = new System.Messaging.Message("Shutdown message, the purpose is to shutdown a process on a remote PC");
MessageQueueTransaction trans = new MessageQueueTransaction();
try
{
trans.Begin();
rmTmsq.Send(msg, pcID, trans);
trans.Commit();
}
catch (Exception e)
{
throw e;
}
finally
{
trans.Dispose();
}
}
示例9: RunCmd
// Overloaded method used to store the threads ID and what file gets opened when CustomRun is ran.
public void RunCmd(string target, string fileName, LabClient client)
{
var pID = "";
using (var process = new Process())
{
process.StartInfo.FileName = target;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
var output = new StringBuilder();
var error = new StringBuilder();
using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
if (e.Data.Contains("ID"))
{
var index = e.Data.IndexOf("ID");
var temp = e.Data.Substring(index, e.Data.Length - index - 1);
pID = temp.Substring(3, temp.Length - 3);
var cp1 = new CompAndProcesses();
cp1.computer = client;
cp1.processName = fileName;
cp1.threadID = pID;
CompAndProcesseses.Add(cp1);
//Send a message to LabClients, if LabClients puts a respond message in the replyqueue.
var path = "FormatName:Direct=OS:" + client.ComputerName.ToLower() + "\\private$\\requestqueue";
MessageQueue rmTmsq = new MessageQueue(path);
System.Messaging.Message msg = new System.Messaging.Message("The purpose is to check whenever a process has been started");
MessageQueueTransaction trans = new MessageQueueTransaction();
try
{
trans.Begin();
rmTmsq.Send(msg, pID, trans);
trans.Commit();
}
catch (Exception ee)
{
throw ee;
}
finally
{
trans.Dispose();
}
}
error.AppendLine(e.Data);
}
};
try
{
process.Start();
}
catch (Exception e)
{
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();
var timeout = 15000;
if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{
//Could type some Process.Exist code here but it aint needed.
Debug.Write(output);
Debug.Write("ERROR:" + error);
}
}
}
}
示例10: AddFrameworkJob
/// <summary>
/// Add a job for the framework to process
/// </summary>
/// <param name="jobName">The name of the job in the framework's workflow file</param>
/// <param name="message">A class containing the message data</param>
public static void AddFrameworkJob(string jobName, IWorkflowMessage message)
{
// Add a message to the Queue
ProcessorJob processorJob = new ProcessorJob() { JobName = jobName, CreatedDate = DateTime.Now };
WorkflowConfiguration.LoadFrameworkConfig(processorJob);
ProcessorQueue processorQueue = GetActiveQueue(processorJob, QueueOperationType.Delivery);
MessageQueue workflowQueue = new MessageQueue(processorQueue.MessageQueue);
MessageQueueTransaction transaction = new MessageQueueTransaction();
try
{
if (processorQueue.MessageQueueType == MessageQueueType.Transactional)
{
transaction.Begin();
workflowQueue.Send(message, jobName, transaction);
transaction.Commit();
}
else
{
workflowQueue.Send(message, jobName);
}
}
catch (Exception e)
{
if (processorQueue.MessageQueueType == MessageQueueType.Transactional &&
transaction.Status == MessageQueueTransactionStatus.Pending)
transaction.Abort();
throw new WorkflowException("Error adding message to Queue", e);
}
finally
{
transaction.Dispose();
workflowQueue.Dispose();
}
}
示例11: PerformHouseKeeping
// Missing fragments are marked as rejected in a separate transaction and are sent to sender's DLQ on commit.
private void PerformHouseKeeping(string correlationId)
{
string subQueueName = Utilities.GetSubQueueName(correlationId);
string subQueuePath = "FormatName:DIRECT=OS:" + this.queue.MachineName + @"\" + this.queue.QueueName + ";" + subQueueName;
MessageQueue subQueue = new MessageQueue(subQueuePath);
Message messageToReject = null;
// MarkMessageRejected works only with transaction
MessageQueueTransaction rejectTransaction = new MessageQueueTransaction();
rejectTransaction.Begin();
// ReceiveById to clear the first header fragment whose Id is put in all other fragment's correlation id
// This is in a separate try-catch so that it proceeds with marking other fragments in case first fragment
// is the one that is lost
try
{
messageToReject = subQueue.ReceiveById(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
catch (MessageQueueException)
{
// Don't do anything
}
catch (InvalidOperationException)
{
// Don't do anything
}
// Marks other fragments in subqueue as rejected
try
{
while (true)
{
messageToReject = subQueue.ReceiveByCorrelationId(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
}
catch (MessageQueueException)
{
// Don't do anything and just come out of the loop
}
catch (InvalidOperationException)
{
// Don't do anything and just come out of the loop
}
// Safe reject in case message becomes available in main queue
try
{
messageToReject = this.queue.ReceiveById(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(subQueue.FormatName, messageToReject.LookupId);
}
catch (MessageQueueException)
{
// Don't do anything
}
catch (InvalidOperationException)
{
// Don't do anything
}
// Mark other fragments in main queue as rejected
try
{
while (true)
{
messageToReject = this.queue.ReceiveByCorrelationId(correlationId, rejectTransaction);
NativeMethods.MarkMessageRejected(this.queue.FormatName, messageToReject.LookupId);
}
}
catch (MessageQueueException)
{
// Don't do anything and just come out of the loop
}
catch (InvalidOperationException)
{
// Don't do anything and just come out of the loop
}
finally
{
subQueue.Dispose();
subQueue.Close();
rejectTransaction.Commit();
rejectTransaction.Dispose();
}
}
示例12: ReceiveGeneric
// Main method that performs the receive operation
// If normal message, returns message to application
// If large message, performs reassembly and return message to application
// This method throws LargeMessageQueueException and delete fragments if holes are found
private Message ReceiveGeneric(string correlationId, TimeSpan timeout, MessageQueueTransaction transaction, MessageQueueTransactionType transactionType)
{
MessageQueueTransaction internalTransaction = transaction;
bool transactionFlag = (internalTransaction != null) ? true : false;
MessageQueueTransaction deleteTransaction = null;
// Holds the initial state of the filter value. this is done so that this filter
// is reverted back to its initial state (initial = when it entered the API)
bool receiveFilterCorrelationId = this.queue.MessageReadPropertyFilter.CorrelationId;
bool receiveFilterAppSpecific = this.queue.MessageReadPropertyFilter.AppSpecific;
bool receiveFilterBody = this.queue.MessageReadPropertyFilter.Body;
bool receiveFilterLookupId = this.queue.MessageReadPropertyFilter.LookupId;
this.queue.MessageReadPropertyFilter.CorrelationId = true;
this.queue.MessageReadPropertyFilter.AppSpecific = true;
this.queue.MessageReadPropertyFilter.Body = true;
this.queue.MessageReadPropertyFilter.LookupId = true;
try
{
// Creates an internal transaction if there was no transaction from the application
if (!transactionFlag)
{
internalTransaction = new MessageQueueTransaction();
internalTransaction.Begin();
}
// Holds the message Id of first fragment
string largeSequenceId = string.Empty;
LargeMessageProperties largeMessageProperties = null;
MessageQueue subQueue = null;
// The overall send formatting is BinaryMessageFormatter, so the receive should also be that
IMessageFormatter formatter = new BinaryMessageFormatter();
int checkPeekMessageStatus = (int)Parameters.ReceiveAction.Restart;
int statusMessageAppSpecific = (int)Parameters.ReceiveAction.None;
do
{
// First gets the status message from the status queue, reference the sub-queue using the
// identifier in status message, perform corresponding receive/reject operation
this.stream = new MemoryStream();
bool lookForStatusMessage = true;
Message statusMessage = null;
while (lookForStatusMessage)
{
try
{
// API firsts peeks from status queue and then receives/deletes the status message.
// This is done to clear junk status messages that may have come to occupy the status queue
statusMessage = this.PeekFromStatusQueue(correlationId);
statusMessage.Formatter = formatter;
statusMessageAppSpecific = statusMessage.AppSpecific;
largeSequenceId = (string)statusMessage.Body;
if (statusMessageAppSpecific == (int)Parameters.ReceiveAction.Delete)
{
try
{
deleteTransaction = new MessageQueueTransaction();
deleteTransaction.Begin();
statusMessage = this.ReceiveFromStatusQueue(statusMessage.LookupId, deleteTransaction, MessageQueueTransactionType.None, false);
statusMessage.Formatter = formatter;
throw new LargeMessageQueueException(largeSequenceId, "Fragments missing");
}
catch (InvalidOperationException)
{
deleteTransaction.Abort();
deleteTransaction.Dispose();
lookForStatusMessage = true;
}
}
else
{
checkPeekMessageStatus = (int)Parameters.ReceiveAction.None;
}
try
{
subQueue = this.GetSubQueue(largeSequenceId);
// throws exception if subqueue empty
this.message = subQueue.PeekByLookupId(MessageLookupAction.Last, 0);
this.message.Formatter = formatter;
try
{
statusMessage = this.ReceiveFromStatusQueue(statusMessage.LookupId, internalTransaction, transactionType, false);
statusMessage.Formatter = formatter;
lookForStatusMessage = false;
}
catch (InvalidOperationException)
{
lookForStatusMessage = true;
}
//.........这里部分代码省略.........
示例13: SendMessage
public static void SendMessage(string message, MessageQueue msgQueue)
{
MessageQueue msgLocal = msgQueue;
System.Messaging.Message msg = new System.Messaging.Message(message);
MessageQueueTransaction transaction = new MessageQueueTransaction();
try
{
transaction.Begin();
msgLocal.Send(msg, Environment.MachineName, transaction);
transaction.Commit();
}
catch (System.Exception e)
{
// cancel the transaction.
transaction.Abort();
// propagate the exception.
throw e;
}
finally
{
// dispose of the transaction object.
transaction.Dispose();
}
}