public static async Task Update()
        {
            try
            {
                var routes   = new ConcurrentBag <ProxyRoute>();
                var clusters = new ConcurrentBag <Cluster>();

                ApplicationList apps = null;
                do
                {
                    apps = await _fabricClient.QueryManager.GetApplicationPagedListAsync(new System.Fabric.Description.ApplicationQueryDescription()
                    {
                        MaxResults = Int32.MaxValue
                    });

                    await apps.AsyncParallelForEach(async app =>
                    {
                        ServiceList services = null;

                        do
                        {
                            services = await _fabricClient.QueryManager.GetServicePagedListAsync(new System.Fabric.Description.ServiceQueryDescription(app.ApplicationName)
                            {
                                MaxResults = Int32.MaxValue
                            });

                            await services.AsyncParallelForEach(async service =>
                            {
                                var cluster     = new Cluster();
                                var serviceName = service.ServiceName.ToString().Replace("fabric:/", "");
                                cluster.Id      = serviceName;
                                clusters.Add(cluster);
                                var destinations = new ConcurrentDictionary <string, Destination>();

                                { // Add Catch All
                                    var route        = new ProxyRoute();
                                    route.RouteId    = serviceName + ":catch-all";
                                    route.ClusterId  = serviceName;
                                    route.Match.Path = serviceName + "/{**catch-all}";
                                    route.Transforms = new List <IDictionary <string, string> >();
                                    route.AddTransformPathRemovePrefix(new AspNetCore.Http.PathString("/" + serviceName));
                                    route.AddTransformRequestHeader("X-Forwarded-PathBase", "/" + serviceName);

                                    routes.Add(route);
                                }
                                { // Add root match
                                    var route        = new ProxyRoute();
                                    route.RouteId    = serviceName + ":root-match";
                                    route.ClusterId  = serviceName;
                                    route.Match.Path = serviceName;
                                    route.Transforms = new List <IDictionary <string, string> >();
                                    route.AddTransformPathRemovePrefix(new AspNetCore.Http.PathString("/" + serviceName));
                                    route.AddTransformRequestHeader("X-Forwarded-PathBase", "/" + serviceName);
                                    routes.Add(route);
                                }

                                ServicePartitionList partitions = null;

                                do
                                {
                                    partitions = partitions == null ?
                                                 await _fabricClient.QueryManager.GetPartitionListAsync(service.ServiceName) :
                                                 await _fabricClient.QueryManager.GetPartitionListAsync(app.ApplicationName, services.ContinuationToken);

                                    await partitions.AsyncParallelForEach(async partition =>
                                    {
                                        var partitionId             = partition.PartitionInformation.Id;
                                        ServiceReplicaList replicas = null;

                                        do
                                        {
                                            replicas = replicas == null ?
                                                       await _fabricClient.QueryManager.GetReplicaListAsync(partitionId) :
                                                       await _fabricClient.QueryManager.GetReplicaListAsync(partitionId, services.ContinuationToken);

                                            await replicas.AsyncParallelForEach(async replica =>
                                            {
                                                var endpointSet = JsonSerializer.Deserialize <ReplicaAddress>(replica.ReplicaAddress);
                                                foreach (var endpoint in endpointSet.Endpoints)
                                                {
                                                    var destination     = new Destination();
                                                    destination.Address = endpoint.Value;
                                                    destinations.TryAdd($"{partitionId}:{replica.Id}", destination);
                                                }
                                            });
                                        }while (!string.IsNullOrEmpty(replicas.ContinuationToken));
                                    });
                                }while (!string.IsNullOrEmpty(partitions.ContinuationToken));
                                foreach (var dest in destinations)
                                {
                                    cluster.Destinations.Add(dest);
                                }
                            });
                        }while (!string.IsNullOrEmpty(services.ContinuationToken));
                    });
                }while (!string.IsNullOrEmpty(apps.ContinuationToken));


                var config    = new ServiceFabricConfig(clusters.ToList(), routes.ToList());
                var oldConfig = _config;
                _config = config;
                oldConfig.SignalChange();
            }catch (Exception ex)
            {
            }
        }
 public ServiceFabricConfigProvider(IConfiguration config)
 {
     _clusterConnection = config["ServiceFabricClusterConnection"];
     _config            = new ServiceFabricConfig();
     _fabricClient      = new FabricClient(_clusterConnection);
 }