Created
July 27, 2011 21:35
-
-
Save DavidBoike/1110423 to your computer and use it in GitHub Desktop.
IsTransactional(true) Rollback Test APp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Transactions; | |
using NServiceBus; | |
using NServiceBus.Config; | |
using NServiceBus.Config.ConfigurationSource; | |
using NServiceBus.Utils; | |
namespace ExceptionTest | |
{ | |
/// <summary> | |
/// Proof of concept that when .IsTransactional(true), throwing an exception | |
/// before completing a transaction will cause any message sent within that | |
/// transaction to be recalled. | |
/// See thread at: http://tech.groups.yahoo.com/group/nservicebus/message/11123 | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MsmqUtilities.CreateQueueIfNecessary("ExceptionTest.CheckHere"); | |
IBus bus = Configure.With() | |
.DefaultBuilder() | |
.XmlSerializer() | |
.UnicastBus() | |
.LoadMessageHandlers() | |
.ImpersonateSender(false) | |
.CustomConfigurationSource(new RmsApplicationConfigurationSource("ExceptionTest")) | |
.MsmqTransport() | |
.IsTransactional(true) | |
.PurgeOnStartup(false) | |
.CreateBus() | |
.Start(); | |
Console.WriteLine("(S)end, e(X)ception, or (Q)uit:"); | |
int id = 0; | |
while (true) | |
{ | |
ConsoleKeyInfo key = Console.ReadKey(true); | |
if (key.Key == ConsoleKey.Q) // Quit | |
return; | |
else if(key.Key == ConsoleKey.S || key.Key == ConsoleKey.X) // Send or eXception | |
{ | |
id++; | |
try | |
{ | |
using (TransactionScope ts = new TransactionScope()) | |
{ | |
bus.Send("ExceptionTest.CheckHere", new DuhMessage { ID = id }); | |
if (key.Key == ConsoleKey.X) | |
throw new ApplicationException("Oops"); | |
ts.Complete(); | |
Console.WriteLine("Message {0} Sent", id); | |
} | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("Error for ID {0}", id); | |
} | |
} | |
} | |
} | |
} | |
public class DuhMessage : IMessage | |
{ | |
public int ID { get; set; } | |
} | |
public class RmsApplicationConfigurationSource : IConfigurationSource | |
{ | |
private readonly string _localQueueName; | |
public RmsApplicationConfigurationSource(string localQueueName) | |
{ | |
_localQueueName = localQueueName; | |
} | |
public T GetConfiguration<T>() where T : class | |
{ | |
if (typeof(T) == typeof(MsmqTransportConfig)) | |
{ | |
var ret = new MsmqTransportConfig | |
{ | |
ErrorQueue = _localQueueName + "errors", | |
InputQueue = _localQueueName, | |
MaxRetries = 5, | |
NumberOfWorkerThreads = 1 | |
}; | |
return ret as T; | |
} | |
throw new NotSupportedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment