diff --git a/src/NServiceBus.AcceptanceTests/Basic/When_using_ineedinitialization.cs b/src/NServiceBus.AcceptanceTests/Basic/When_using_ineedinitialization.cs new file mode 100644 index 00000000000..cd28866b418 --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/Basic/When_using_ineedinitialization.cs @@ -0,0 +1,101 @@ +namespace NServiceBus.AcceptanceTests.Basic +{ + using System; + using NServiceBus.AcceptanceTesting; + using NServiceBus.AcceptanceTests.EndpointTemplates; + using NUnit.Framework; + + public class When_using_ineedinitialization : NServiceBusAcceptanceTest + { + [Test] + public void Should_be_able_to_set_endpoint_name() + { + var context = new Context(); + + Scenario.Define(context) + .WithEndpoint() + .WithEndpoint() + .Done(c => c.WasCalled) + .Run(); + + Assert.True(context.WasCalled, "The message handler should be called"); + } + + public class Context : ScenarioContext + { + public bool WasCalled { get; set; } + } + + public class Sender : EndpointConfigurationBuilder + { + public Sender() + { + EndpointSetup(); + } + } + + public class Receiver : EndpointConfigurationBuilder + { + public Receiver() + { + EndpointSetup() + .AddMapping(typeof(Sender)); + } + + public class SetEndpointName : INeedInitialization + { + public void Customize(BusConfiguration config) + { + config.EndpointName("ineedinitialization_receiver"); + } + } + + public class SendMessageToSender: IWantToRunWhenBusStartsAndStops + { + public IBus Bus { get; set; } + + public void Start() + { + Bus.Send(new SendMessage()); + } + + public void Stop() + { + } + } + } + + [Serializable] + public class SendMessage : ICommand + { + } + + [Serializable] + public class MyMessage : ICommand + { + public Guid Id { get; set; } + } + + public class SendMessageHandler : IHandleMessages + { + public IBus Bus { get; set; } + + public void Handle(SendMessage message) + { + Bus.Send("ineedinitialization_receiver", new MyMessage()); + } + } + + public class MyMessageHandler : IHandleMessages + { + public Context Context { get; set; } + + public IBus Bus { get; set; } + + public void Handle(MyMessage message) + { + Context.WasCalled = true; + } + } + } +} diff --git a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj index 92181ed356b..f5c3a0c32cc 100644 --- a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj +++ b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj @@ -52,6 +52,7 @@ + @@ -67,6 +68,7 @@ + diff --git a/src/NServiceBus.AcceptanceTests/Tx/Issue_2481.cs b/src/NServiceBus.AcceptanceTests/Tx/Issue_2481.cs new file mode 100644 index 00000000000..0678b25115a --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/Tx/Issue_2481.cs @@ -0,0 +1,58 @@ +namespace NServiceBus.AcceptanceTests.Tx +{ + using System; + using System.Transactions; + using NServiceBus.AcceptanceTesting; + using NServiceBus.AcceptanceTests.EndpointTemplates; + using NServiceBus.AcceptanceTests.ScenarioDescriptors; + using NUnit.Framework; + + public class Issue_2481 : NServiceBusAcceptanceTest + { + [Test] + public void Should_enlist_the_receive_in_the_dtc_tx() + { + Scenario.Define() + .WithEndpoint(b => b.Given(bus => bus.SendLocal(new MyMessage()))) + .Done(c => c.HandlerInvoked) + .Repeat(r => r.For()) + .Should(c => Assert.False(c.CanEnlistPromotable, "There should exists a DTC tx")) + .Run(); + } + + + public class Context : ScenarioContext + { + public bool HandlerInvoked { get; set; } + + public bool CanEnlistPromotable { get; set; } + } + + public class DTCEndpoint : EndpointConfigurationBuilder + { + public DTCEndpoint() + { + EndpointSetup(c=>c.Transactions().Enable()); + } + + public class MyMessageHandler : IHandleMessages + { + public Context Context { get; set; } + + public void Handle(MyMessage messageThatIsEnlisted) + { + Context.CanEnlistPromotable = Transaction.Current.EnlistPromotableSinglePhase(new FakePromotableResourceManager()); + Context.HandlerInvoked = true; + } + } + } + + [Serializable] + public class MyMessage : ICommand + { + } + + + + } +} diff --git a/src/NServiceBus.Core/BusConfiguration.cs b/src/NServiceBus.Core/BusConfiguration.cs index ec85a5d6dcb..6c9f19ff8fa 100644 --- a/src/NServiceBus.Core/BusConfiguration.cs +++ b/src/NServiceBus.Core/BusConfiguration.cs @@ -33,6 +33,13 @@ public BusConfiguration() : base(new SettingsHolder()) configurationSourceToUse = new DefaultConfigurationSource(); Settings.Set(new PipelineModifications()); Pipeline = new PipelineSettings(this); + + Settings.SetDefault("Endpoint.SendOnly", false); + Settings.SetDefault("Transactions.Enabled", true); + Settings.SetDefault("Transactions.IsolationLevel", IsolationLevel.ReadCommitted); + Settings.SetDefault("Transactions.DefaultTimeout", TransactionManager.DefaultTimeout); + Settings.SetDefault("Transactions.SuppressDistributedTransactions", false); + Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", false); } /// @@ -193,9 +200,29 @@ internal Configure BuildConfiguration() } } + Settings.SetDefault("TypesToScan", scannedTypes); + + Configure.ActivateAndInvoke(scannedTypes, t => t.Customize(this)); + UseTransportExtensions.SetupTransport(this); var container = customBuilder ?? new AutofacObjectBuilder(); - RegisterEndpointWideDefaults(); + + Settings.SetDefault(configurationSourceToUse); + + var endpointHelper = new EndpointHelper(new StackTrace()); + + if (endpointVersion == null) + { + endpointVersion = endpointHelper.GetEndpointVersion(); + } + + if (endpointName == null) + { + endpointName = endpointHelper.GetDefaultEndpointName(); + } + + Settings.SetDefault("EndpointName", endpointName); + Settings.SetDefault("EndpointVersion", endpointVersion); if (publicReturnAddress != null) { @@ -206,8 +233,6 @@ internal Configure BuildConfiguration() Settings.SetDefault(conventionsBuilder.Conventions); - Configure.ActivateAndInvoke(scannedTypes, t => t.Customize(this)); - return new Configure(Settings, container, registrations, Pipeline); } @@ -224,32 +249,6 @@ IEnumerable GetAssembliesInDirectory(string path, params string[] asse .Assemblies; } - void RegisterEndpointWideDefaults() - { - var endpointHelper = new EndpointHelper(new StackTrace()); - - if (endpointVersion == null) - { - endpointVersion = endpointHelper.GetEndpointVersion(); - } - - if (endpointName == null) - { - endpointName = endpointHelper.GetDefaultEndpointName(); - } - - Settings.SetDefault("EndpointName", endpointName); - Settings.SetDefault("TypesToScan", scannedTypes); - Settings.SetDefault("EndpointVersion", endpointVersion); - Settings.SetDefault("Endpoint.SendOnly", false); - Settings.SetDefault("Transactions.Enabled", true); - Settings.SetDefault("Transactions.IsolationLevel", IsolationLevel.ReadCommitted); - Settings.SetDefault("Transactions.DefaultTimeout", TransactionManager.DefaultTimeout); - Settings.SetDefault("Transactions.SuppressDistributedTransactions", false); - Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", false); - Settings.SetDefault(configurationSourceToUse); - } - IConfigurationSource configurationSourceToUse; ConventionsBuilder conventionsBuilder = new ConventionsBuilder(); List> registrations = new List>(); diff --git a/src/NServiceBus.Core/Settings/TransactionSettings.cs b/src/NServiceBus.Core/Settings/TransactionSettings.cs index 5082acee0f1..241d64b2bc7 100644 --- a/src/NServiceBus.Core/Settings/TransactionSettings.cs +++ b/src/NServiceBus.Core/Settings/TransactionSettings.cs @@ -23,7 +23,7 @@ internal TransactionSettings(BusConfiguration config) public TransactionSettings Disable() { config.Settings.Set("Transactions.Enabled", false); - config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", false); + config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", true); config.Settings.SetDefault("Transactions.SuppressDistributedTransactions", true); return this; @@ -35,7 +35,7 @@ public TransactionSettings Disable() public TransactionSettings Enable() { config.Settings.Set("Transactions.Enabled", true); - config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", true); + config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", false); config.Settings.SetDefault("Transactions.SuppressDistributedTransactions", false); return this; @@ -61,7 +61,7 @@ public TransactionSettings IsolationLevel(IsolationLevel isolationLevel) public TransactionSettings DisableDistributedTransactions() { config.Settings.Set("Transactions.SuppressDistributedTransactions", true); - config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", false); + config.Settings.SetDefault("Transactions.DoNotWrapHandlersExecutionInATransactionScope", true); return this; } diff --git a/src/NServiceBus.Core/Transports/Msmq/MsmqMessageSender.cs b/src/NServiceBus.Core/Transports/Msmq/MsmqMessageSender.cs index 3174b4bacfe..176bf758469 100644 --- a/src/NServiceBus.Core/Transports/Msmq/MsmqMessageSender.cs +++ b/src/NServiceBus.Core/Transports/Msmq/MsmqMessageSender.cs @@ -97,11 +97,6 @@ MessageQueueTransactionType GetTransactionTypeForSend() return MessageQueueTransactionType.None; } - if (SuppressDistributedTransactions) - { - return MessageQueueTransactionType.Single; - } - return Transaction.Current != null ? MessageQueueTransactionType.Automatic : MessageQueueTransactionType.Single;