private async Task <IActionResult> RunScript(Endpoint endpoint, Dictionary <string, object> parameters = null, bool noSerialization = false) { try { var variables = new Dictionary <string, object> { { "Request", Request }, { "Response", Response }, { "User", HttpContext?.User?.Identity?.Name }, { "MemoryCache", _memoryCache }, { "UDConnectionManager", _connectionManager } }; try { if (HttpContext.Request.Cookies.ContainsKey("location") == true) { var location = HttpContext.Request.Cookies["location"]; if (!string.IsNullOrEmpty(location)) { location = Encoding.UTF8.GetString(Convert.FromBase64String(location)); var locationObject = JsonConvert.DeserializeObject <Location>(location); variables.Add("Location", locationObject); } } } catch (Exception ex) { Log.Error("RunScript() Exception processing geolocation. " + ex.Message); } ExecutionContext executionContext = new ExecutionContext(endpoint, variables, parameters, HttpContext?.User); executionContext.NoSerialization = noSerialization; if (HttpContext.Request.Headers.TryGetValue("UDConnectionId", out StringValues connectionId)) { executionContext.SessionId = _connectionManager.GetSessionId(connectionId); executionContext.ConnectionId = connectionId; } var result = await _executionService.ExecuteEndpointAsync(executionContext, endpoint); var actionResult = ConvertToActionResult(result); return(actionResult); } catch (Exception ex) { Log.Warn("RunScript() " + ex.Message + Environment.NewLine + ex.StackTrace); throw ex; } }
public async Task ClientEvent(string eventId, string eventName, string eventData, string location) { _logger.Debug($"ClientEvent {eventId} {eventName}"); var variables = new Dictionary <string, object>(); var userName = Context.User?.Identity?.Name; if (!string.IsNullOrEmpty(userName)) { variables.Add("user", userName); } if (!string.IsNullOrEmpty(location)) { location = Encoding.UTF8.GetString(Convert.FromBase64String(location)); var locationObject = JsonConvert.DeserializeObject <Location>(location); variables.Add("Location", locationObject); } if (bool.TryParse(eventData, out bool data)) { variables.Add("EventData", data); } else { variables.Add("EventData", eventData); } variables.Add("UDConnectionManager", _connectionManager); variables.Add("EventId", eventId); variables.Add("MemoryCache", _memoryCache); try { var sessionId = _connectionManager.GetSessionId(Context.ConnectionId); var endpoint = _dashboardService.EndpointService.Get(eventId, sessionId); if (endpoint == null) { _logger.Warn($"Endpoint {eventId} not found."); throw new Exception($"Endpoint {eventId} not found."); } var executionContext = new ExecutionContext(endpoint, variables, new Dictionary <string, object>(), Context.User); executionContext.ConnectionId = Context.ConnectionId; executionContext.SessionId = sessionId; try { dynamic result = await _executionService.ExecuteEndpointAsync(executionContext, endpoint); if (result.Error is Error error) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Clients.Client(Context.ConnectionId).SendAsync("showError", new { message = error.Message }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } } catch (RuntimeBinderException) { } catch (Exception ex) { _logger.Error("Failed to execute action. " + ex.Message); throw; } } catch (Exception ex) { _logger.Warn($"Failed to execute endpoint. " + ex.Message); throw; } }