public static Task _DispatchCommandInternalAsync(ICommand message, Type messageType, bool requireAffirmation) { ICommandClient client = null; Type messageBaseType = messageType; while (client == null && messageBaseType != null) { if (commandClients.TryGetValue(messageBaseType, out client)) { if (requireAffirmation) { return(client.DispatchAsyncAwait(message)); } else { return(client.DispatchAsync(message)); } } messageBaseType = messageBaseType.BaseType; } if (requireAffirmation) { return(HandleCommandAsync((ICommand)message, messageType, true)); } else { //Task.Run execues on shared principal which can mess things up return(TaskSafePrincipal.Run(async() => { await HandleCommandAsync((ICommand)message, messageType, true); })); } }