public static async Task Update() { var clusters = new List <Cluster>(); var routes = new List <ProxyRoute>(); var config = new ServiceFabricConfig(clusters, routes); using (var client = new HttpClient()) { var strApps = await client.GetStringAsync($"{_sfUri}/Applications?api-version=3.0"); var appResponse = await JsonSerializer.DeserializeAsync <ServiceFabricResponse <Application> >(new MemoryStream(Encoding.UTF8.GetBytes(strApps))); foreach (var app in appResponse.Items) { var appName = app.Name.Replace("fabric:/", ""); var strService = await client.GetStringAsync($"{_sfUri}/Applications/{appName}/$/GetServices?api-version=3.0"); var serviceResponse = await JsonSerializer.DeserializeAsync <ServiceFabricResponse <Service> >(new MemoryStream(Encoding.UTF8.GetBytes(strService))); foreach (var service in serviceResponse.Items) { var serviceName = service.Name.Replace($"fabric:/", ""); var strPartitions = await client.GetStringAsync($"{_sfUri}/Applications/{appName}/$/GetServices/{serviceName}/$/GetPartitions?api-version=3.0"); var partitionResponse = await JsonSerializer.DeserializeAsync <ServiceFabricResponse <Partition> >(new MemoryStream(Encoding.UTF8.GetBytes(strPartitions))); var cluster = new Cluster(); cluster.Id = serviceName; clusters.Add(cluster); { // 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.Transforms.Add(new Dictionary <string, string>() { { "PathRemovePrefix", 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.Transforms.Add(new Dictionary <string, string>() { { "PathRemovePrefix", serviceName } }); route.AddTransformRequestHeader("X-Forwarded-PathBase", "/" + serviceName); routes.Add(route); } foreach (var partition in partitionResponse.Items) { var partitionId = partition.PartitionInformation.Id; var strReplicas = await client.GetStringAsync($"{_sfUri}/Applications/{appName}/$/GetServices/{serviceName}/$/GetPartitions/{partitionId}/$/GetReplicas?api-version=3.0"); var replicasResponse = await JsonSerializer.DeserializeAsync <ServiceFabricResponse <Replica> >(new MemoryStream(Encoding.UTF8.GetBytes(strReplicas))); foreach (var replica in replicasResponse.Items) { var replicaAddress = await JsonSerializer.DeserializeAsync <ReplicaAddress>(new MemoryStream(Encoding.UTF8.GetBytes(replica.Address))); foreach (var endpoint in replicaAddress.Endpoints) { var destination = new Destination(); destination.Address = endpoint.Value; cluster.Destinations.Add($"{partitionId}:{replica.InstanceId}", destination); } } } } } var oldConfig = _config; _config = config; oldConfig.SignalChange(); } }
public ServiceFabricConfigRESTProvider(IConfiguration config) { _sfUri = config["ServiceFabricUri"]; _config = new ServiceFabricConfig(); }