Пример #1
0
        /// <summary>
        /// Gets the details for all applications or for a specific application created in the system.
        /// Also takes in timeout interval, which is the maximum of time the system will allow this operation to continue before returning.
        /// </summary>
        public async Task <IEnumerable <ApplicationWrapper> > GetApplicationListAsync(Uri applicationNameFilter, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var             applicationList = new List <ApplicationWrapper>();
            ApplicationList previousResult  = null;

            // Set up the counter that record the time lapse.
            var stopWatch = ValueStopwatch.StartNew();

            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                var remaining = timeout - stopWatch.Elapsed;
                if (remaining.Ticks < 0)
                {
                    // If the passing time is longer than the timeout duration.
                    throw new TimeoutException($"Unable to enumerate all application pages in the allotted time budget of {timeout.TotalSeconds} seconds");
                }

                previousResult = await ExceptionsHelper.TranslateCancellations(
                    () => _queryClient.GetApplicationListAsync(
                        applicationNameFilter: applicationNameFilter,
                        continuationToken: previousResult?.ContinuationToken,
                        timeout: remaining,
                        cancellationToken: cancellationToken),
                    cancellationToken);

                applicationList.AddRange(previousResult.Select(MapApp));
            }while (!string.IsNullOrEmpty(previousResult?.ContinuationToken));

            return(applicationList);

            ApplicationWrapper MapApp(Application app) =>
            new ApplicationWrapper
            {
                ApplicationName        = app.ApplicationName,
                ApplicationTypeName    = app.ApplicationTypeName,
                ApplicationTypeVersion = app.ApplicationTypeVersion,
                ApplicationParameters  = MapAppParameters(app),
            };

            IDictionary <string, string> MapAppParameters(Application app)
            {
                // NOTE: App Params in Service Fabric are case insensitive (verified on version 7.0.457.9590).
                // Since this is not documented behavior, the code below tries to play it safe by ignoring
                // duplicated app params instead of throwing and preventing such service from working at all
                // behind the Proxy.
                var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                foreach (var param in app.ApplicationParameters)
                {
                    if (!result.TryAdd(param.Name, param.Value))
                    {
                        Log.DuplicateAppParameter(_logger, param.Name, app.ApplicationName);
                    }
                }

                return(result);
            }
        }