NServiceBus: Could not find Metadata for (message)
Asked Answered
C

2

7

I am attempting to publish a message as shown below

_bus.Publish(new BatchCompleted { BatchId = batch.Id});

And handle it in a BatchCompletedHandler:

public class BatchCompletedHandler: IHandleMessages<BatchCompleted>
{

    public void Handle(BatchCompleted message)
    {
          Do Some Stuff...
    }
}

Whenever I try and publish the message I get the following System.Exception:

Could not find Metadata for 'MyAssembly.BatchCompleted'. Messages need to implement either 'IMessage', 'IEvent' or 'ICommand'. Alternatively, if you don't want to implement an interface, you can configure 'Unobtrusive Mode Messages' and use convention to configure how messages are mapped.

The message does implement IEvent as shown below

[Serializable]
public class BatchCompleted : IEvent
{
    public int BatchId{ get; set; }
}

I am configuring NSB using the following code

Configure.With(MyAssembly)

The message handler is in the assembly MyAssembly and the messages are in the MyMessagesAssembly.

What am I doing wrong?

Ceremonious answered 24/9, 2013 at 11:59 Comment(2)
Can you post your configuration? Is your UnicastBusConfig, Message mappings configured appropriately? Is your messages assembly using the same .net version? i.e. is your handler using .net 4.0 and your messages a .net 4.5?Gerkman
Thanks Indu, for the pointer. It was the configuration causing the problem!Ceremonious
C
5

NServiceBus cannot find the message type being used. The messages are in a separate assembly but the configuration causes NSB to only scan the assembly specified (MyAssembly) rather than the default behaviour of scanning all assemblies in the binaries folder.

Changing the configuration to

Configure.With()

will scan all assemblies and allow NSB to find the required message types.

I think the error message is a little misleading!

Ceremonious answered 30/9, 2013 at 9:12 Comment(0)
I
5

In NSB 5 you can scan all assemblies using AssembliesToScan as explained in NServiceBus doc: http://docs.particular.net/nservicebus/hosting/assembly-scanning

If you have specified namespaces conventions, make sure that all rules are correct:

config.Conventions()
            .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.StartsWith("..."))
            .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("..."))
            .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.StartsWith("..."));

I hope this will helpful

Intransigence answered 14/9, 2016 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.