Automating deployment to Azure

4 years ago
How can I get the app to startup properly with a clean database and a properly populated connection string in Settings.txt?  When the app first starts, I get "No store could be loaded".  The Web Platform Installer has the same problem if Settings.txt is not blank when the app starts.

My goal is to allow deployments without the user worrying about the database connection string.
4 years ago
FYI.  I have modified the code to solve my problem.  

The first step was to modify Nop.Core.Data.DataSettingsHelper.DatabaseIsInstalled() to return false if the Customer table is empty.  This will force the app to display the Install page.  The second step was to modify Nop.Web.Controllers.InstallController.Index() to use the Settings.txt value to initialize the InstallModel object if the connection string successfully connects to the database.

In an ideal solution, the SQL connection string would be hidden entirely, but part of the goal here was to make minimal changes so that they can be easily applied to any future builds.  

The changes are as follows:

        public static bool DatabaseIsInstalled()
        {
            if (!_databaseIsInstalled.HasValue)
            {
                var manager = new DataSettingsManager();
                var settings = manager.LoadSettings();
                _databaseIsInstalled = settings != null && !String.IsNullOrEmpty(settings.DataConnectionString);
                try
                {
                    if ( _databaseIsInstalled.Value )
                    {
                        using (var con = new System.Data.SqlClient.SqlConnection(settings.DataConnectionString))
                        {
                            con.Open();
                            using ( var cmd = con.CreateCommand() )
                            {
                                cmd.CommandText = "select count(*) from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'Customer'";
                                int nTables = Convert.ToInt32(cmd.ExecuteScalar());
                                if ( nTables > 0 )
                                {
                                    cmd.CommandText = "select count(*) from Customer";
                                    int nUsers = Convert.ToInt32(cmd.ExecuteScalar());
                                    if ( nUsers == 0 )
                                    {
                                        _databaseIsInstalled = false;
                                    }
                                }
                                else
                                {
                                    _databaseIsInstalled = false;
                                }
                            }
                        }
                    }
                }
                catch ( Exception ex )
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _databaseIsInstalled = false;
                }
            }
            return _databaseIsInstalled.Value;
        }


        public ActionResult Index()
        {
            if (DataSettingsHelper.DatabaseIsInstalled())
                return RedirectToRoute("HomePage");

            //set page timeout to 5 minutes
            this.Server.ScriptTimeout = 300;

            var model = new InstallModel
            {
                AdminEmail = "[email protected]",
                InstallSampleData = false,
                DatabaseConnectionString = "",
                DataProvider = "sqlserver",
                //fast installation service does not support SQL compact
                DisableSqlCompact = !String.IsNullOrEmpty(ConfigurationManager.AppSettings["UseFastInstallationService"]) &&
                    Convert.ToBoolean(ConfigurationManager.AppSettings["UseFastInstallationService"]),
                DisableSampleDataOption = !String.IsNullOrEmpty(ConfigurationManager.AppSettings["DisableSampleDataDuringInstallation"]) &&
                    Convert.ToBoolean(ConfigurationManager.AppSettings["DisableSampleDataDuringInstallation"]),
                SqlAuthenticationType = "sqlauthentication",
                SqlConnectionInfo = "sqlconnectioninfo_values",
                SqlServerCreateDatabase = false,
                UseCustomCollation = false,
                Collation = "SQL_Latin1_General_CP1_CI_AS",
            };

            var manager = new DataSettingsManager();
            var settings = manager.LoadSettings();
            bool databaseIsInstalled = settings != null && !String.IsNullOrEmpty(settings.DataConnectionString);
            try
            {
                if ( databaseIsInstalled )
                {
                    using (var conn = new System.Data.SqlClient.SqlConnection(settings.DataConnectionString))
                    {
                        conn.Open();
                    }
                    model.DisableSqlCompact = true;
                    model.SqlConnectionInfo = "sqlconnectioninfo_raw";
                    model.DatabaseConnectionString = settings.DataConnectionString;
                    string[] arrParts = model.DatabaseConnectionString.Split(';');
                    foreach ( string sPart in arrParts )
                    {
                        string[] arrNameValue = sPart.Split('=');
                        if ( arrNameValue.Length >= 2 )
                        {
                            switch ( arrNameValue[0].ToLower() )
                            {
                                case "server"  :  model.SqlServerName     = arrNameValue[1];  break;
                                case "database":  model.SqlServerUsername = arrNameValue[1];  break;
                                case "user id" :  model.SqlDatabaseName   = arrNameValue[1];  break;
                                case "password":  model.SqlServerPassword = arrNameValue[1];  break;
                            }
                        }
                    }
                }
            }
            catch
            {
            }

            foreach (var lang in _locService.GetAvailableLanguages())
            {
                model.AvailableLanguages.Add(new SelectListItem
                {
                    Value = Url.Action("ChangeLanguage", "Install", new { language = lang.Code}),
                    Text = lang.Name,
                    Selected = _locService.GetCurrentLanguage().Code == lang.Code,
                });
            }

            return View(model);
        }