protected object GetEnvironmentVariable(Type objectType, string schemaName)
        {
            var queryVariable = BindingModelHelper.GetRetrieveAllQuery <EnvironmentVariable>();

            queryVariable.Criteria.AddCondition(EnvironmentVariableDefinition.Columns.SchemaName, ConditionOperator.Equal, schemaName);

            var linkValue = queryVariable.AddLink(EnvironmentVariableValueDefinition.EntityName, EnvironmentVariableDefinition.Columns.Id, EnvironmentVariableValueDefinition.Columns.EnvironmentVariableDefinitionId, JoinOperator.LeftOuter);

            linkValue.EntityAlias = EnvironmentVariableValueDefinition.EntityName;
            linkValue.Columns.AddColumn(EnvironmentVariableValueDefinition.Columns.Value);

            var variable = AdminOrganizationService.RetrieveAll <EnvironmentVariable>(queryVariable).FirstOrDefault();

            if (variable == null)
            {
                return(null);
            }

            switch (variable.Type)
            {
            case EnvironmentVariableType.String:
                if (objectType != typeof(string))
                {
                    throw new ArgumentException($"The environment variable is of type String GetEnvironmentVariable must be called with a string Type argument");
                }

                return(variable.Value);

            case EnvironmentVariableType.Number:
                if (objectType != typeof(int))
                {
                    throw new ArgumentException($"The environment variable is of type Integer GetEnvironmentVariable must be called with a int Type argument");
                }

                return(int.Parse(variable.Value));

            case EnvironmentVariableType.Boolean:
                if (objectType != typeof(bool))
                {
                    throw new ArgumentException($"The environment variable is of type Boolean GetEnvironmentVariable must be called with a bool Type argument");
                }

                return(bool.Parse(variable.Value));

            case EnvironmentVariableType.Json:
                return(JsonConvert.DeserializeObject(variable.Value, objectType));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private bool SendToRemoteDebugger(LocalWorkflowContext localContext, CodeActivityContext context)
        {
            if (!localContext.IsDebugContext)
            {
                localContext.Log("The context is genuine");

                var initiatingUserId = localContext.GetInitiatingUserId();

                var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery <DebugSession>();
                queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.Equal, initiatingUserId);
                queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode, ConditionOperator.Equal, DebugSessionState.Active.ToInt());

                var debugSession = localContext.AdminOrganizationService.RetrieveAll <DebugSession>(queryDebugSessions).FirstOrDefault();

                localContext.Log($"Debug session : {debugSession}");

                if (debugSession != null)
                {
                    if (debugSession.SessionEnd >= DateTime.Today)
                    {
                        var remoteContext = localContext.RemoteContext;

                        remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName;
                        remoteContext.Id = Guid.NewGuid();

                        SetArgumentsInRemoteContext(context, remoteContext);

                        var uri = new Uri($"{debugSession.RelayUrl}/{debugSession.HybridConnectionName}");

                        try
                        {
                            using (var hybridConnection = new HybridConnection(debugSession.SasKeyName, debugSession.SasConnectionKey, uri.AbsoluteUri))
                            {
                                var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext, remoteContext.Id);

                                RemoteDebuggerMessage response;
                                while (true)
                                {
                                    localContext.Log("Sending context to local machine : {0}", message);

                                    response = hybridConnection.SendMessage(message).GetAwaiter().GetResult();

                                    localContext.Log("Received response : {0}", response);

                                    if (response.MessageType == RemoteDebuggerMessageType.Context || response.MessageType == RemoteDebuggerMessageType.Exception)
                                    {
                                        break;
                                    }

                                    var request = response.GetOrganizationRequest();

                                    var service = response.UserId.HasValue ? localContext.GetService(response.UserId.Value) : localContext.AdminOrganizationService;

                                    var organizationResponse = service.Execute(request);

                                    message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response, organizationResponse, remoteContext.Id);
                                }

                                if (response.MessageType == RemoteDebuggerMessageType.Exception)
                                {
                                    throw response.GetException();
                                }

                                var updatedContext = response.GetContext <RemoteDebugExecutionContext>();

                                localContext.UpdateContext(updatedContext);

                                ExtractArgumentsFromRemoteContext(context, updatedContext, localContext.LogServiceMethod);
                            }


                            return(true);
                        }
                        catch (HttpRequestException e)
                        {
                            // Run the plugin as deploy if the remote debugger is not connected
                            localContext.Log($"Error while sending context : {e}");
                        }
                    }
                }
            }

            return(false);
        }
        private bool SendToRemoteDebugger(LocalPluginContext localContext)
        {
            if (!localContext.IsDebugContext)
            {
                localContext.Log("The context is genuine");
                localContext.Log($"UnSecuredConfig : {UnSecuredConfig}");

                //if (!string.IsNullOrEmpty(UnSecuredConfig) && UnSecuredConfig.Contains("debugSessions"))
                //{
                //    var debuggerUnsecuredConfig = JsonConvert.DeserializeObject<DebuggerUnsecureConfig>(UnSecuredConfig);

                //    localContext.Log($"Debug session ids : {string.Join(",", debuggerUnsecuredConfig.DebugSessionIds)}");

                var initiatingUserId = localContext.GetInitiatingUserId();

                localContext.Log($"Initiating user Id : {initiatingUserId}");

                var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery <DebugSession>();
                queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.Equal, initiatingUserId);
                queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode, ConditionOperator.Equal, DebugSessionState.Active.ToInt());

                //queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.Id, ConditionOperator.In, debuggerUnsecuredConfig.DebugSessionIds.Cast<object>().ToArray());

                var debugSession = localContext.AdminOrganizationService.RetrieveAll <DebugSession>(queryDebugSessions).FirstOrDefault();

                localContext.Log($"Debug session : {debugSession}");

                if (debugSession != null)
                {
                    if (debugSession.SessionEnd >= DateTime.Today)
                    {
                        var remoteContext = localContext.RemoteContext;
                        remoteContext.Id = Guid.NewGuid();
                        remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName;
                        remoteContext.UnsecureConfig            = UnSecuredConfig;
                        remoteContext.SecureConfig = SecuredConfig;

                        var uri = new Uri($"{debugSession.RelayUrl}/{debugSession.HybridConnectionName}");

                        try
                        {
                            using var hybridConnection = new HybridConnection(debugSession.SasKeyName, debugSession.SasConnectionKey, uri.AbsoluteUri);
                            var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext, remoteContext.Id);

                            RemoteDebuggerMessage response;
                            while (true)
                            {
                                localContext.Log("Sending context to local machine : {0}", message);

                                response = hybridConnection.SendMessage(message).GetAwaiter().GetResult();

                                localContext.Log("Received response : {0}", response);

                                if (response.MessageType == RemoteDebuggerMessageType.Context || response.MessageType == RemoteDebuggerMessageType.Exception)
                                {
                                    break;
                                }

                                var request = response.GetOrganizationRequest();

                                var service = response.UserId.HasValue ? localContext.GetService(response.UserId.Value) : localContext.AdminOrganizationService;

                                var organizationResponse = service.Execute(request);

                                message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response, organizationResponse, remoteContext.Id);
                            }

                            if (response.MessageType == RemoteDebuggerMessageType.Exception)
                            {
                                throw response.GetException();
                            }

                            var updatedContext = response.GetContext <RemoteDebugExecutionContext>();

                            localContext.UpdateContext(updatedContext);


                            return(true);
                        }
                        catch (HttpRequestException)
                        {
                            // Run the plugin as deploy if the remote debugger is not connected
                        }
                    }
                }
                //}
            }

            return(false);
        }