T CallEndpoint <T>(Func <GatewayEndpoint, T> callToInvoke) { T result = default(T); bool exitCall = false; while (!exitCall) { GatewayEndpoint endpoint = SelectEndpoint(); if (endpoint != null) { try { result = callToInvoke.Invoke(endpoint); exitCall = true; } catch (Exception) { endpoint.IsActive = false; } } else { exitCall = true; throw new MissingGatewayEndpointException("No available gateway data found to achieve service call."); } } return(result); }
public T Call <T>(params object[] args) { if (Endpoints == null || Endpoints.Count() == 0) { return(default(T)); } GatewayEndpoint endpoint = Endpoints.ToList()[0]; // the routes should all be the same on all the endpoints bool exit = false; List <string> arguments = new List <string>(); string route = endpoint.Route; while (!exit) { int startPos = route.IndexOf("{"); if (startPos > -1) { int endPos = route.Substring(startPos).IndexOf("}"); if (endPos > -1) { string argument = route.Substring(startPos + 1, endPos - 1); arguments.Add(argument); route = route.Substring(startPos + endPos + 1); } } else { exit = true; } } dynamic payload = new ExpandoObject(); IDictionary <string, object> dictPayload = payload as IDictionary <string, object>; int index = 0; foreach (string argument in arguments) { if (args.Length >= index + 1) { dictPayload[argument] = args[index]; index++; } } SetPayload(payload); return(Call <T>()); }
public void SetEndpoint(GatewayEndpoint endpoint) { List <GatewayEndpoint> endpoints = Endpoints?.ToList(); if (endpoints == null) { endpoints = new List <GatewayEndpoint>(); } endpoint.IsActive = true; endpoints.Add(endpoint); Endpoints = endpoints; }
GatewayEndpoint SelectEndpoint() { IEnumerable <GatewayEndpoint> activeEndpoints = Endpoints.Where(item => item.IsActive == true); if (activeEndpoints.Count() > 0) { Random rnd = new Random(); int itemIndex = rnd.Next(0, activeEndpoints.Count()); GatewayEndpoint endpoint = activeEndpoints.ToList()[itemIndex]; endpoint.LastCall = DateTime.Now; return(endpoint); } else { return(null); } }