NServicebus发布事件 - 收到空消息

时间:2011-07-22 13:29:49

标签: domain-driven-design nservicebus cqrs eda event-store

在我的域中发布事件后,我继续从我的denormalizer主机收到以下消息:

2011-07-22 14:18:32,374 [Worker.5] WARN  NServiceBus.Unicast.UnicastBus [(null)]
 <(null)> - Received an empty message - ignoring.

我正在使用jolivers eventstore执行此操作:

return Wireup.Init()
                .UsingRavenPersistence("EventStore", new DocumentObjectSerializer())
                .UsingAsynchronousDispatcher()
                .PublishTo(new DelegateMessagePublisher(c => container.Resolve<IPublishMessages>().Publish(c)))
                .Build();

IPublishMessages解析为:

 public sealed class NServiceBusPublisher : IPublishMessages
{
    private const string AggregateIdKey = "AggregateId";
    private const string CommitVersionKey = "CommitVersion";
    private const string EventVersionKey = "EventVersion";
    private const string BusPrefixKey = "Bus.";
    private readonly IBus bus;

    public NServiceBusPublisher(IBus bus)
    {
        this.bus = bus;
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }

    public void Publish(Commit commit)
    {
        for (var i = 0; i < commit.Events.Count; i++)
        {
            var eventMessage = commit.Events[i];
            var busMessage = eventMessage.Body as IMessage;
            AppendHeaders(busMessage, commit.Headers); // optional
            AppendHeaders(busMessage, eventMessage.Headers); // optional
            AppendVersion(commit, i); // optional
            this.bus.Publish(busMessage);
        }
    }
    private static void AppendHeaders(IMessage message, IEnumerable<KeyValuePair<string, object>> headers)
    {
        headers = headers.Where(x => x.Key.StartsWith(BusPrefixKey));
        foreach (var header in headers)
        {
            var key = header.Key.Substring(BusPrefixKey.Length);
            var value = (header.Value ?? string.Empty).ToString();
            message.SetHeader(key, value);
        }
    }
    private static void AppendVersion(Commit commit, int index)
    {
        var busMessage = commit.Events[index].Body as IMessage;
        busMessage.SetHeader(AggregateIdKey, commit.StreamId.ToString());
        busMessage.SetHeader(CommitVersionKey, commit.StreamRevision.ToString());
        busMessage.SetHeader(EventVersionKey, GetSpecificEventVersion(commit, index).ToString());
    }
    private static int GetSpecificEventVersion(Commit commit, int index)
    {
        // e.g. (StreamRevision: 120) - (5 events) + 1 + (index @ 4: the last index) = event version: 120
        return commit.StreamRevision - commit.Events.Count + 1 + index;
    }
}

发布事件的NServiceBus主机具有以下配置:

    <MsmqTransportConfig
    InputQueue="SonatribeInputQueue"
    ErrorQueue="error"
    NumberOfWorkerThreads="1"
    MaxRetries="5"
    />

  <UnicastBusConfig
     DistributorControlAddress=""
     DistributorDataAddress=""
     ForwardReceivedMessagesTo="">
    <MessageEndpointMappings>
      <add Messages="Events" Endpoint="SonatribeInputQueue"/>
    </MessageEndpointMappings>
  </UnicastBusConfig>

接收事件的denormalizer主机是:

 <MsmqTransportConfig
    InputQueue="MyServerInputQueue"
    ErrorQueue="error"
    NumberOfWorkerThreads="1"
    MaxRetries="5"
    />

  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="Events" Endpoint="SonatribeInputQueue"/>
    </MessageEndpointMappings>
  </UnicastBusConfig>

对我来说似乎是正确的 - 发布者处理从网络发送的命令,域引发了事件。 NServiceBusPublisher发布事件和非规范化主机至少收到一些东西!这不是它发送的消息 - 或者至少发生了一些事情,认为消息是空的......

任何?

** * ** 更新 * ** * ** * ****

我现在从l4n获得以下内容:

2011-07-22 16:31:30,646 [Worker.7] ERROR NServiceBus.Faults.InMemory.FaultManage
r [(null)] <(null)> - Serialization failed for message with ID 56b7b693-090f-49b
d-83ad-7beeb334fe6d\12639.
System.Security.VerificationException: Operation could destabilize the runtime.
   at SetId(Object , Object )
   at NServiceBus.Serializers.XML.MessageSerializer.GetObjectOfTypeFromNode(Type
 t, XmlNode node) in c:\Dev\NServiceBus\src\impl\Serializers\NServiceBus.Seriali
zers.XML\MessageSerializer.cs:line 343
   at NServiceBus.Serializers.XML.MessageSerializer.Process(XmlNode node, Object
 parent) in c:\Dev\NServiceBus\src\impl\Serializers\NServiceBus.Serializers.XML\
MessageSerializer.cs:line 330
   at NServiceBus.Serializers.XML.MessageSerializer.Deserialize(Stream stream) i
n c:\Dev\NServiceBus\src\impl\Serializers\NServiceBus.Serializers.XML\MessageSer
ializer.cs:line 267
   at NServiceBus.Unicast.UnicastBus.Extract(TransportMessage m) in c:\Dev\NServ
iceBus\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 851
2011-07-22 16:31:30,908 [Worker.7] WARN  NServiceBus.Unicast.UnicastBus [(null)]
 <(null)> - Received an empty message - ignoring.

以下是它试图反序列化的消息!!

[Serializable]
public class AccountCreatedEvent : IEvent
{
    public readonly string Id;
    public readonly string Name;

    public AccountCreatedEvent()
    {

    }

    public AccountCreatedEvent(string id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
    public override string ToString()
    {
        return "AccountCreatedEvent - for when you've created an account";
    }
}

并不是什么忙乱......

1 个答案:

答案 0 :(得分:5)

很清楚为什么该消息无法反序列化 - 您只有readonly属性。如果您希望这项工作,则需要切换到公共获取/设置属性。为了保留您不可公开更改的属性的意图,这里有另一种方法:

[Serializable]
public class AccountCreatedEvent : IEvent
{
    public string Id { get; private set; }
    public string Name {get; private set; }

    public AccountCreatedEvent(string id, string name)
    {
        this.Id = id;
        this.Name = name;
    }

    public override string ToString()
    {
        return "AccountCreatedEvent - for when you've created an account";
    }
}