From 23bab6025f4d6c814058544b89b0d1335706bfc4 Mon Sep 17 00:00:00 2001 From: John Simons Date: Mon, 27 Oct 2014 11:45:07 +1000 Subject: [PATCH] INeedInitialization not called early enough. This causes transport, conventions, endpointname and endpointversion not being picked up if configured in INeedInitialization. Fixes #2530 --- .../Basic/When_using_ineedinitialization.cs | 101 ++++++++++++++++++ .../NServiceBus.AcceptanceTests.csproj | 3 +- src/NServiceBus.Core/BusConfiguration.cs | 8 +- 3 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 src/NServiceBus.AcceptanceTests/Basic/When_using_ineedinitialization.cs 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 3d7d81a48e4..f5c3a0c32cc 100644 --- a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj +++ b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj @@ -1,4 +1,4 @@ - + @@ -52,6 +52,7 @@ + diff --git a/src/NServiceBus.Core/BusConfiguration.cs b/src/NServiceBus.Core/BusConfiguration.cs index 94642aff84b..6c9f19ff8fa 100644 --- a/src/NServiceBus.Core/BusConfiguration.cs +++ b/src/NServiceBus.Core/BusConfiguration.cs @@ -200,11 +200,14 @@ internal Configure BuildConfiguration() } } + Settings.SetDefault("TypesToScan", scannedTypes); + + Configure.ActivateAndInvoke(scannedTypes, t => t.Customize(this)); + UseTransportExtensions.SetupTransport(this); var container = customBuilder ?? new AutofacObjectBuilder(); Settings.SetDefault(configurationSourceToUse); - Settings.SetDefault("TypesToScan", scannedTypes); var endpointHelper = new EndpointHelper(new StackTrace()); @@ -221,7 +224,6 @@ internal Configure BuildConfiguration() Settings.SetDefault("EndpointName", endpointName); Settings.SetDefault("EndpointVersion", endpointVersion); - if (publicReturnAddress != null) { Settings.SetDefault("PublicReturnAddress", publicReturnAddress); @@ -231,8 +233,6 @@ internal Configure BuildConfiguration() Settings.SetDefault(conventionsBuilder.Conventions); - Configure.ActivateAndInvoke(scannedTypes, t => t.Customize(this)); - return new Configure(Settings, container, registrations, Pipeline); }