Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when doing a send and no mapping is configured #1875

Closed
SimonCropp opened this issue Dec 29, 2013 · 2 comments
Closed
Labels
Milestone

Comments

@SimonCropp
Copy link
Contributor

This case is now explicitly detected and a better exception is thrown.

So this

Configure.Serialization.Json();
var bus = Configure.With()
                .DefaultBuilder()
                .UseTransport<Msmq>()
                .InMemorySagaPersister()
                .UnicastBus()
                .CreateBus()
                .Start(() => Configure.Instance.ForInstallationOn<Windows>().Install());
bus.Send(new MyMessage());
Console.ReadLine();

With no mapping configured for MyMessage

Will result in a NullReferenceException with the following stack trace

   at NServiceBus.Transports.Msmq.MsmqUtilities.GetFullPath(Address value) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Transports\Msmq\MsmqUtilities.cs:line 27
   at NServiceBus.Transports.Msmq.MsmqMessageSender.Send(TransportMessage message, Address address) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Transports\Msmq\MsmqMessageSender.cs:line 38
   at NServiceBus.Unicast.Behaviors.DispatchMessageToTransportBehavior.Invoke(SendPhysicalMessageContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\DispatchMessageToTransportBehavior.cs:line 76
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.<>c__DisplayClass1.<InvokeNext>b__0() in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.MessageMutator.MutateOutgoingPhysicalMessageBehavior.Invoke(SendPhysicalMessageContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\MessageMutator\MutateOutgoingPhysicalMessageBehavior.cs:line 19
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.<>c__DisplayClass1.<InvokeNext>b__0() in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Unicast.Behaviors.SerializeMessagesBehavior.Invoke(SendPhysicalMessageContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\SerializeMessagesBehavior.cs:line 40
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.Invoke(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 15
   at NServiceBus.Pipeline.PipelineFactory.Execute[T](BehaviorChain`1 pipelineAction, T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 166
   at NServiceBus.Pipeline.PipelineFactory.InvokeSendPipeline(SendOptions sendOptions, TransportMessage physicalMessage) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 130
   at NServiceBus.Unicast.Behaviors.CreatePhysicalMessageBehavior.Invoke(SendLogicalMessagesContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\CreatePhysicalMessageBehavior.cs:line 52
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.<>c__DisplayClass1.<InvokeNext>b__0() in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Unicast.Behaviors.MultiMessageBehavior.Invoke(SendLogicalMessagesContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\MultiMessageBehavior.cs:line 19
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.<>c__DisplayClass1.<InvokeNext>b__0() in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Unicast.Behaviors.MultiSendValidatorBehavior.Invoke(SendLogicalMessagesContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\MultiSendValidatorBehavior.cs:line 33
   at NServiceBus.Pipeline.BehaviorChain`1.InvokeNext(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 28
   at NServiceBus.Pipeline.BehaviorChain`1.Invoke(T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 15
   at NServiceBus.Pipeline.PipelineFactory.Execute[T](BehaviorChain`1 pipelineAction, T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 166
   at NServiceBus.Pipeline.PipelineFactory.InvokeSendPipeline(SendOptions sendOptions, IEnumerable`1 messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 97
   at NServiceBus.Unicast.UnicastBus.InvokeSendPipeline(SendOptions sendOptions, List`1 messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 728
   at NServiceBus.Unicast.UnicastBus.SendMessages(SendOptions sendOptions, List`1 messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 704
   at NServiceBus.Unicast.UnicastBus.Send(Object[] messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 550
   at NServiceBus.Unicast.UnicastBus.Send(Object message) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 534

We should give a better exception

@johnsimons
Copy link
Member

Here is the exception we used to throw in v3:

System.InvalidOperationException: No destination specified for message(s): MyClient.Foo
   at NServiceBus.Unicast.UnicastBus.<>c__DisplayClass16.<SendMessage>b__11(Addr
ess address) in c:\TeamCity\buildAgent\work\nsb.master_0\src\unicast\NServiceBus
.Unicast\UnicastBus.cs:line 715
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
...

I suggest we do the same.

@SimonCropp
Copy link
Contributor Author

here is a acceptance test to help repro

Note that this is not a real test. just a helper to reproduce and verify the fix

public class FakeTest : NServiceBusAcceptanceTest
{
    [Test]
    public void Should_receive_the_message()
    {
        Scenario.Define(() => new Context())
                .WithEndpoint<Sender>(b => b.Given((bus, context) => bus.Send(new MyMessage())))
                .WithEndpoint<Receiver>()
                .Done(c => c.WasCalled)
                .Run();
    }

    public class Context : ScenarioContext
    {
        public bool WasCalled { get; set; }
    }

    public class Sender : EndpointConfigurationBuilder
    {
        public Sender()
        {
            EndpointSetup<DefaultServer>()
                // uncomment this line and it works
                //.AddMapping<MyMessage>(typeof(Receiver))
                ;
        }
    }

    public class Receiver : EndpointConfigurationBuilder
    {
        public Receiver()
        {
            EndpointSetup<DefaultServer>();
        }
    }

    public class MyMessage : ICommand
    {
        public Guid Id { get; set; }
    }

    public class MyMessageHandler : IHandleMessages<MyMessage>
    {
        public Context Context { get; set; }

        public IBus Bus { get; set; }

        public void Handle(MyMessage message)
        {
            Context.WasCalled = true;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants