public static bool CheckDatabaseConnection(string connectionId, Func <DatabaseConnectionConfiguration, bool> validateConfigFunc, bool allowConfiguration, bool forceConfiguration = false)
        {
            LoggerService.LogDebugAsync("CheckRegisteredDatabasesConnections", () => new { allowConfiguration, forceConfiguration, connectionId }).Wait();
            if ((allowConfiguration || forceConfiguration) && DatabaseConnectionConfigurator == null)
            {
                var logmessage = "DatabaseConnectionConfigurator is null";
                LoggerService.LogAsync(logmessage, ELogType.Error, new { allowConfiguration, forceConfiguration, connectionId }).Wait();
                throw new InvalidOperationException(logmessage);
            }

            var connectionSucceeded = false;
            DatabaseConnectionConfiguration config = null;

            try
            {
                config = DatabaseConnectionManager.GetConfiguration(connectionId);
            }
            catch (DatabaseConnectionException)
            {
                config = new DatabaseConnectionConfiguration()
                {
                    ConnectionId = connectionId
                };
            }

            //do
            {
                try
                {
                    // Try the first time without configuring
                    if (forceConfiguration || !Validate())
                    {
                        // If it fails, try to configure, and then check again
                        if ((allowConfiguration || forceConfiguration) && DatabaseConnectionConfigurator.Configure(ref config))
                        {
                            DatabaseConnectionManager.SaveConfiguration(config);
                            connectionSucceeded = Validate();
                        }
                    }
                    else
                    {
                        connectionSucceeded = true;
                    }
                }
                catch (Exception ex)
                {
                    LoggerService.LogAsync(ex).Wait();
                }
            } //while (!connectionSucceeded && allowConfiguration);

            return(connectionSucceeded);

            bool Validate()
            {
                try
                {
                    return(validateConfigFunc(config));
                }
                catch (Exception dbe)
                {
                    LoggerService.Log(dbe);
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
        public static object GetDataContext(Type dataContextType, string connectionID, bool useSharedDataContext = true)
        {
            CheckInitializationNeeded();

            if (!useSharedDataContext)
            {
                return(DataContextHandler.GetDataContextOfType(dataContextType, DatabaseConnectionManager.GetConfiguration(connectionID)));
            }
            else
            {
                if (!_threadLocalObjects.Value.ContainsKey(connectionID))
                {
                    var contexto = DataContextHandler.GetDataContextOfType(dataContextType, DatabaseConnectionManager.GetConfiguration(connectionID));
                    _threadLocalObjects.Value.Add(connectionID, contexto);
                }

                return(_threadLocalObjects.Value[connectionID]);
            }
        }