3

我有一个看起来像这样的处理程序:

public class CreateNewUserHandler :
    Saga<UncorroboratedCreateNewUser>,
    IAmStartedByMessages<CreateNewUser>,
    IHandleMessages<FoundUser>
{

    [Dependency]
    public IBus Bus { get; set; }


    public override void ConfigureHowToFindSaga()
    {        
        ConfigureMapping<CreateNewUser>(saga => saga.CorrelationId, req => req.CorrelationId);
        ConfigureMapping<FoundUser>(saga => saga.CorrelationId, foundUser => foundUser.CorrelationId); //CorrelationId is of type Guid here
    }

    public void Handle(CreateNewUser message)
    {
        Mapper.DynamicMap(message, Data, typeof(CreateNewUser), typeof(UncorroboratedCreateNewUser));
        Data.CorrelationId = message.CorrelationId;
        Bus.Send(new FindUserByUserName { CorrelationId = Data.CorrelationId, UserName = message.UserName });
    }


    public void Handle(FoundUser message)
    {
        //**THIS BLOCK WAS NEVER HIT**
    }
}

现在应该回复的另一个处理程序FoundUser是这样的:

public class FindUserByUserNameHandler : IMessageHandler<FindUserByUserName>
{
    private readonly UserRepository _userRepository;
    public IBus Bus { get; set; }

    public FindUserByUserNameHandler(UserRepository  userRepository)
    {
        _userRepository = userRepository;
    }

    public void Handle(FindUserByUserName message)
    {
        var foundUser  = _userRepository.FindByUserName(message.UserName);
        FoundUser result = Bus.CreateInstance<FoundUser>( _ => _.CorrelationId = message.CorrelationId);
        if (foundUser != null)
        {
            result = Mapper.DynamicMap<FoundUser>(foundUser);
            result.IsUserFound = true;
        }
        else
        {
            result.
                AuthenticationUserName = message.UserName;
            result.IsUserFound = false;
        }

        Bus.Reply(result);
    }
}

在调试中,我已经能够跟踪进入 CreateNewUser => FindUserByName => Reply 的消息,并且观察跟踪日志,它甚至看起来像是消息返回到原始队列。

但是该方法void Handle(FoundUser message)从未被调用!我已经失去了一个晚上的睡眠,破坏了我的大脑并在互联网上寻找关于我可能在哪里丢球的线索。如果将 CreateNewUserHandler 转换为常规处理程序(非 saga),则另一件事是调用上面的方法!

这些是我必须继续下去的唯一线索(而且不多——我真的希望这个错误更有意义)

2013-05-17 14:30:29,682 [Worker.18] WARN MyProject.Unicast.Transport.Transactional.TransactionalTransport [(null)] <(null)> - 未能为 ID=377f1e49 的消息引发“收到传输消息”事件-06e2-465f-877a-9443828e8866 System.NullReferenceException:对象引用未设置为对象的实例。在 C:\TeamCity\buildAgent\work\nsb.master_7\src\unicast\NServiceBus.Unicast\UnicastBus.cs:NServiceBus.Unicast.UnicastBus 中的 NServiceBus.Unicast.UnicastBus.HandleTransportMessage(IBuilder childBuilder, TransportMessage msg) 处。 TransportMessageReceived(Object sender, TransportMessageReceivedEventArgs e) in c:\TeamCity\buildAgent\work\nsb.master_7\src\unicast\NServiceBus.Unicast\UnicastBus.cs: System.EventHandler`1.Invoke(Object sender, TEventArgs e) 的第 1248 行) 在 NServiceBus.Unicast。

并且

2013-05-17 14:30:29,591 [Worker.18] INFO NServiceBus.Sagas.Impl.SagaDispatcherFactory [(null)] <(null)> - 找不到带有 id 的消息类型 MyProject.Messages.FoundUser 的传奇377f1e49-06e2-465f-877a-9443828e8866。将调用 SagaNotFoundHandlers。

以防万一需要配置:

NServiceBus.Configure.With(busAssemblies)
                        .Log4Net()
                        .License(Config.Default.NServiceBus_License)
                        .DefineEndpointName(endPointName) 
                        .UnityBuilder(serviceBusDiConfiguration.Container)
                        .DontUseTransactions() //I don't know why this is needed, but doesn't seem to get very far otherwise.
                        .AzureConfigurationSource()
                        .AzureSagaPersister()
                        .AzureSubcriptionStorage()
                        .AzureDataBus()
                        .JsonSerializer()
                        .AzureServiceBusMessageQueue()
                        .UnicastBus()
                        .LoadMessageHandlers()
                        .CreateBus()
                        .Start();
   BusConfiguration.Configurer.ConfigureComponent(uoWImplementer, DependencyLifecycle.InstancePerUnitOfWork); //For the custom unitOfWork

请帮忙!

=====================下面的消息 DTO===========================

public class CreateNewUser : ICommand
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public Guid CorrelationId { get; set; }
}

public class FindUserByUserName : IMessage
{
    public Guid CorrelationId { get; set; }
    public string UserName { get; set; }
}

public class FoundUser: IMessage
{
    public bool IsUserFound { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string AuthenticationUserName { get; set; }
    public Guid CorrelationId { get; set; }
}

而传奇类本身:

public class UncorroboratedCreateNewUser : IContainSagaData
{
    public virtual Guid Id { get; set; }
    public virtual string Originator { get; set; }
    public virtual string OriginalMessageId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }
    public virtual Guid CorrelationId { get; set; }
}
4

2 回答 2

5

有几件事让我大吃一惊。

首先,尝试在 Configure.With() 之后和 .CreateBus() 之前在初始化代码中调用 .Sagas()。

此外,删除对 saga 的 Bus 依赖项 - NServiceBus 已经在 saga 类上定义了该依赖项。

删除 CreateNewUser 的 ConfigureMapping 调用(除非您希望在每个 saga 中收到此消息的多个实例)。

最后,[Unique]在您的 saga 数据的 CorrelationID 属性上放置一个属性(以保证如果消息被并行处理,您最终不会得到多个 saga)。

于 2013-05-18T06:02:58.393 回答
0

我认为映射代码会覆盖CorrelationId

result = Mapper.DynamicMap<FoundUser>(foundUser);

分配CorrelationId最后一个

于 2013-05-18T04:08:12.787 回答