public static async Task <SelectedPartition> GetSelectedPartitionStateAsync(
            FabricClient fabricClient,
            PartitionSelector partitionSelector,
            TimeSpan requestTimeout,
            TimeSpan operationTimeout,
            CancellationToken cancellationToken)
        {
            ThrowIf.Null(partitionSelector, "PartitionSelector");

            Guid partitionId = Guid.Empty;
            Uri  serviceName;

            if (!partitionSelector.TryGetPartitionIdIfNotGetServiceName(out partitionId, out serviceName))
            {
                // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors.  We do not want to retry "service not found".
                ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>(
                    () => fabricClient.QueryManager.GetPartitionListAsync(
                        serviceName,
                        null,
                        default(string),
                        requestTimeout,
                        cancellationToken),
                    operationTimeout,
                    cancellationToken).ConfigureAwait(false);

                Partition partitionResult = partitionSelector.GetSelectedPartition(partitionsResult.ToArray(), new Random());
                partitionId = partitionResult.PartitionInformation.Id;
            }
            else
            {
                // Validate the partition specified is actually from the service specified.
                // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors.  We do not want to retry "service not found".
                ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>(
                    () => fabricClient.QueryManager.GetPartitionListAsync(
                        serviceName,
                        null,
                        default(string),
                        requestTimeout,
                        cancellationToken),
                    operationTimeout,
                    cancellationToken).ConfigureAwait(false);

                var guids = partitionsResult.Select(p => p.PartitionId()).ToList();
                if (!guids.Contains(partitionId))
                {
                    // There is no partition in the service specified that has a partition with the id specified.
                    throw new FabricException("Partition not found in this service", FabricErrorCode.PartitionNotFound);
                }
            }

            return(new SelectedPartition(serviceName, partitionId));
        }
            protected override async Task ExecuteActionAsync(FabricTestContext testContext, GetSelectedPartitionStateAction action, CancellationToken cancellationToken)
            {
                ThrowIf.Null(action.PartitionSelector, "PartitionSelector");

                Guid partitionId;
                Uri  serviceName;

                if (!action.PartitionSelector.TryGetPartitionIdIfNotGetServiceName(out partitionId, out serviceName))
                {
                    // TODO: make these actions which store state locally as well.
                    ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>(
                        () =>
                        testContext.FabricClient.QueryManager.GetPartitionListAsync(
                            serviceName,
                            null,
                            default(string),
                            action.RequestTimeout,
                            cancellationToken),
                        action.ActionTimeout,
                        cancellationToken).ConfigureAwait(false);

                    Partition partitionResult = action.PartitionSelector.GetSelectedPartition(partitionsResult.ToArray(), testContext.Random);

                    partitionId = partitionResult.PartitionInformation.Id;
                }
                else
                {
                    // Validate the partition specified is actually from the service specified.
                    // Intentionally do not use FabricClientRetryErrors.GetPartitionListFabricErrors.  We do not want to retry "service not found".
                    ServicePartitionList partitionsResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync <ServicePartitionList>(
                        () => testContext.FabricClient.QueryManager.GetPartitionListAsync(
                            serviceName,
                            null,
                            default(string),
                            action.RequestTimeout,
                            cancellationToken),
                        action.ActionTimeout,
                        cancellationToken).ConfigureAwait(false);

                    var guids = partitionsResult.Select(p => p.PartitionId()).ToList();
                    if (!guids.Contains(partitionId))
                    {
                        // The message in the first arg is only for debugging, it is not returned to the user.
                        throw new FabricException("Partition not found", FabricErrorCode.PartitionNotFound);
                    }
                }

                action.Result = new SelectedPartition(serviceName, partitionId);

                ResultTraceString = StringHelper.Format("PartitionSelector Selected Partition with ID {0}", action.Result);
            }