NServiceBus:找不到元数据(消息)

时间:2013-09-24 11:59:10

标签: .net nservicebus messaging publish-subscribe

我正在尝试发布如下所示的消息

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

并在BatchCompletedHandler中处理它:

public class BatchCompletedHandler: IHandleMessages<BatchCompleted>
{

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

每当我尝试发布消息时,我都会收到以下System.Exception:

  

找不到'MyAssembly.BatchCompleted'的元数据。消息需要实现“IMessage”,“IEvent”或“ICommand”。或者,如果您不想实现接口,则可以配置“Unobtrusive Mode Messages”并使用约定来配置消息的映射方式。

消息 实现IEvent,如下所示

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

我正在使用以下代码配置NSB

Configure.With(MyAssembly)

消息处理程序位于程序集MyAssembly中,消息位于MyMessagesAssembly中。

我做错了什么?

2 个答案:

答案 0 :(得分:5)

NServiceBus无法找到正在使用的消息类型。消息在单独的程序集中,但配置导致NSB仅扫描指定的程序集(MyAssembly),而不是扫描二进制文件夹中所有程序集的默认行为。

将配置更改为

Configure.With()

将扫描所有程序集并允许NSB查找所需的消息类型。

我认为错误信息有点误导!

答案 1 :(得分:2)

在NSB 5中,您可以使用AssembliesToScan扫描所有程序集,如NServiceBus doc中所述: http://docs.particular.net/nservicebus/hosting/assembly-scanning

如果您已指定名称空间约定,请确保所有规则都正确无误:

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("..."));

我希望这会有所帮助

相关问题