gpt4 book ai didi

redis - MassTransit Redis 集成

转载 作者:可可西里 更新时间:2023-11-01 11:18:22 25 4
gpt4 key购买 nike

我开始使用 Masstransit sagas 并构建了一个示例,如果我使用 InMemorySagaRepository 一切正常,但是当使用 Redis 存储库时,Increase 消息被移动到 RabbitMq 中的 Consumer_Error 队列,并显示错误消息:'Value cannot为空。参数名称:key' 和此堆栈跟踪:

at System.Collections.Generic.Dictionary`2.FindEntry(TKey key) 
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at Automatonymous.AutomatonymousStateMachine`1.GetState(String name)
at Automatonymous.Accessors.RawStateAccessor`1.Automatonymous.StateAccessor<TInstance>.Get(InstanceContext`1 context)
at Automatonymous.Accessors.InitialIfNullStateAccessor`1.<Automatonymous-StateAccessor<TInstance>-Get>d__3.MoveNext()
at Automatonymous.Pipeline.StateMachineSagaMessageFilter`2.<Send>d__5.MoveNext()
at GreenPipes.Filters.ConcurrencyLimitFilter`1.<Send>d__5.MoveNext()
at MassTransit.RedisIntegration.RedisSagaRepository`1.<SendToInstance>d__7`1.MoveNext()

这是我的代码:

public class SagaConsumer : MassTransitStateMachine<Number>
{
public SagaConsumer()
{
State(() => Active);

InstanceState(x => x.Status);

Event(() => Created);
Event(() => Increased);

Initially(
When(Created)
.Then(context =>
{
context.Instance.Name = context.Data.Name;
context.Instance.Value = 0;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"New name:{context.Data.Name}"))
.TransitionTo(Active)
);

During(Active,
When(Increased)
.Then(context =>
{
context.Instance.Value = context.Data.Increase ? context.Instance.Value + 1 : context.Instance.Value - 1;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"{context.Instance.Name} increased to value {context.Instance.Value}"))
);
}

public State Active
{
get; set;
}

public Event<ICreateMessage> Created { get; set; }
public Event<IIncreaseMessage> Increased { get; set; }
}

以及配置代码:

var bus = Bus.Factory.CreateUsingRabbitMq(configurator =>
{
var host = configurator.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});

var redisManager = new PooledRedisClientManager("localhost");
configurator.ReceiveEndpoint("Consumer", endpoint =>
{
endpoint.StateMachineSaga(new SagaConsumer(),
//new InMemorySagaRepository<Number>(),
new RedisSagaRepository<Number>(redisManager),
c => c.UseConcurrencyLimit(10));
});
});

和 Saga 类:

public class Number : SagaStateMachineInstance, IHasGuidId, IVersionedSaga, CorrelatedBy<Guid>
{
public Guid CorrelationId
{
get;
set;
}

public string Name { get; set; }
public State Status { get; set; }
public int Value { get; set; }
public int Version { get; set; }

public Guid Id
=> CorrelationId;
}

它似乎与 Number 类的 Status 属性有关,该属性在某些时候为空,但我不确定为什么或为什么仅在使用 Redis 时才会发生

最佳答案

因此,您应该将状态存储为 intstring - 而不是您现在拥有的 State。这将解决您遇到的问题。

关于redis - MassTransit Redis 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41188893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com