[Problem found] Element 'value' not found in CommandHandler


I’ve recently updated to Dolittle v.4.*.
I implemented the IAggregateOf instead of IAggregateRootRepository.
This might have been an issue before the framework update.

This error happens whenever i try to remove the existing object within the application.
I figured this might be a faulty MongoDB object?

After some debugging, I noticed that it crashed on the .Rehydrate(EventSourceId) in my CommandHandler and gave me the error underneath.

Note: The program was able to delete the object I created while being on version 3..
But none of the objects created with v.4.

fail: CommandHandlerInvoker[0]
      [TryHandle(110)]-Failed invoking command handler 'Domain.Admin.Vessels.VesselCommandHandler, Domain, Version=, Culture=neutral, PublicKeyToken=null' for command of type '{[Type: Dolittle.Artifacts.Artifact]
      { Id : d6dd00f4-3c2e-48f0-8a7f-18ff1ce10cba }{ Generation : 1 }}
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException: Element 'value' not found.
   at MongoDB.Bson.BsonDocument.get_Item(String name)
   at Dolittle.Runtime.Events.MongoDB.BsonValueExtensions.ToClaim(BsonValue value)
   at System.Linq.Enumerable.SelectIListIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dolittle.Runtime.Events.MongoDB.BsonValueExtensions.ToClaims(BsonArray array)
   at Dolittle.Runtime.Events.MongoDB.BsonValueExtensions.ToOriginalContext(BsonValue value)
   at Dolittle.Runtime.Events.MongoDB.FromBsonDocumentExtensions.ToEventMetadata(BsonDocument doc)
   at Dolittle.Runtime.Events.MongoDB.FromBsonDocumentExtensions.ToEventEnvelope(BsonDocument doc)
   at Dolittle.Runtime.Events.MongoDB.BsonValueExtensions.ToEventStream(BsonValue value)
   at Dolittle.Runtime.Events.MongoDB.FromBsonDocumentExtensions.ToCommittedEventStream(BsonDocument doc)
   at Dolittle.Runtime.Events.Store.MongoDB.EventStore.FindCommitsWithSorting(FilterDefinition`1 filter)
   at Dolittle.Domain.AggregateRootRepositoryFor`1.ReApplyEvents(T aggregateRoot)
   at Dolittle.Domain.AggregateRootRepositoryFor`1.Get(EventSourceId id)
   at Dolittle.Domain.AggregateOf`1.Rehydrate(EventSourceId eventSourceId)
   at Domain.Admin.Vessels.VesselCommandHandler.Handle(RemoveVessel cmd) in C:\Users\*\Vessels\VesselCommandHandler.cs:line 24
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Dolittle.Commands.Handling.CommandHandlerInvoker.TryHandle(CommandRequest command)

Inside the CommandHandler

namespace Domain.Admin.Vehicles
    public class VehicleCommandHandler : ICanHandleCommands
        readonly IAggregateOf<Vehicles> _aggregateRootRepoForVessel;

        public VehicelCommandHandler(IAggregateOf<Vehicles> repo)
             _repo =  repo;

        public void Handle(RegisterVehicle cmd)
                .Perform(_ => _.RegisterVehicle(cmd.number, cmd.name));

        public void Handle(RemoveVehicle cmd)
            var o =_root.Rehydrate(cmd.VehicleId); 
            o.Perform(_ => _.RemoveVehicle());

Inside the aggregate root:

public void RemoveVehicle()
    Apply(new VehicleRemoved(EventSourceId));

Inside the event

namespace Events.Vechicles
    public class VehicleRemoved : IEvent
        public Guid VechicleId { get; }
        public VehicleRemoved(Guid vehicleId)
            VechicleId = vehicleId;


I can use the code shown in the examples to remove a vehicle, but only when the vehicles was created before the upgrade (so my old objects from the read_model). Have I missed anything that should have been converted between the versions or is there any problems?
Seems to be a miss-match between id’s whenever it tries to find the vehicle after the update.

Back again!

The problem seems to be how the application saves on user claims and had nothing to do with the aggregate root. The event store holds on claims from auth0, which it is not allowed to do.

The workaround was to empty the claims before they enter the event store and gets handled handled as meta-data in a event.

1 Like

I’ve created a bug for this issue (https://github.com/dolittle/Home/issues/68) and submitted a pull request to “fix” it.

The solution is to ignore claims when serializing to and from the Event Store. So that it’s not a breaking change, the property is not being removed from the interface. Events are undergoing a significant (and very breaking!) change in version 5 so this is just a stop-gap measure.

Until this PR is approved and a new version of the MongoDB event store is released, you can get around the bug by removing the Claims from the Execution Context.

On each request, you could set the Execution Context:

var current = _executionContextManager.Current;
_executionContextManger.CurrentFor(current.Tenant, current.CorrelationId, Claims.Empty);

You would still be able to access the claims for this request through the Current Principal.