Пример #1
0
        public void CreateDatabaseInUncPath()
        {
            // This is where cab files will be placed. Also - I'm trying to change the creation so that this is the
            // path that the SQL database will be created in too. For the moment the database will be created in the
            // default location.
            String cabFolder        = @"\\localhost\C$\stackhashunittests\TestCabFolder";
            String connectionString = TestSettings.DefaultConnectionString + "Initial Catalog=MASTER;";
            String databaseName     = "StackHashInstallTestDatabase";


            InstallerInterface sqlInstaller = new InstallerInterface(connectionString, databaseName, cabFolder);


            try
            {
                try
                {
                    // Connect to SQL Server instance.
                    sqlInstaller.Connect();
                }
                catch (System.Exception ex)
                {
                    // Process or log errors here.
                    Console.WriteLine(ex);
                    throw;
                }

                try
                {
                    // Check if the database exists.
                    if (sqlInstaller.DatabaseExists())
                    {
                        return;  // Job done.
                    }
                    sqlInstaller.CreateDatabase(false);

                    Assert.AreEqual(true, sqlInstaller.DatabaseExists());

                    String databaseFileName    = String.Format("{0}\\{1}\\{1}.mdf", cabFolder, databaseName);
                    String databaseLogFileName = String.Format("{0}\\{1}\\{1}.ldf", cabFolder, databaseName);

                    Assert.AreEqual(true, File.Exists(databaseFileName));
                    Assert.AreEqual(true, File.Exists(databaseLogFileName));
                }
                catch (System.Exception ex)
                {
                    // Failed to create the database.
                    // Process or log errors here.
                    Console.WriteLine(ex);
                    throw;
                }
            }
            finally
            {
                sqlInstaller.DeleteDatabase(databaseName);
                sqlInstaller.Disconnect();
            }
        }
Пример #2
0
        void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkerResult       result    = new WorkerResult();
            InstallerInterface installer = null;

            try
            {
                WorkerArg arg = e.Argument as WorkerArg;
                result.TestConnectionOnly = arg.TestConnectionOnly;
                result.TestStatus         = StackHashErrorIndexDatabaseStatus.Unknown;

                // configure for local access to the service
                if (!_localServiceConfigComplete)
                {
                    _localServiceGuid = ClientLogic.GetLocalServiceGuid();
                    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);

                    CheckVersionRequest checkVersionRequest = new CheckVersionRequest();
                    checkVersionRequest.ServiceGuid  = _localServiceGuid == Guid.Empty ? null : _localServiceGuid.ToString();
                    checkVersionRequest.ClientData   = GenerateClientData();
                    checkVersionRequest.MajorVersion = fvi.ProductMajorPart;
                    checkVersionRequest.MinorVersion = fvi.ProductMinorPart;
                    CheckVersionResponse checkVersionResponse = ServiceProxy.Services.Admin.CheckVersion(checkVersionRequest);

                    _localServiceConfigComplete = true;
                }

                // always test that the service can access the database

                StackHashSqlConfiguration sqlConfig = new StackHashSqlConfiguration();
                sqlConfig.ConnectionString  = arg.MasterConnectionString;
                sqlConfig.ConnectionTimeout = DefaultSqlConnectionTimeout;
                sqlConfig.EventsPerBlock    = DefaultSqlEventsPerBlock;
                sqlConfig.InitialCatalog    = arg.ProfileName;
                sqlConfig.MaxPoolSize       = DefaultSqlMaxPoolSize;
                sqlConfig.MinPoolSize       = DefaultSqlMinPoolSize;

                TestDatabaseConnectionRequest request = new TestDatabaseConnectionRequest();
                request.ClientData            = GenerateClientData();
                request.ContextId             = -1;
                request.SqlSettings           = sqlConfig;
                request.TestDatabaseExistence = false;
                request.CabFolder             = arg.CabFolder;

                TestDatabaseConnectionResponse response = ServiceProxy.Services.Admin.TestDatabaseConnection(request);
                result.TestStatus             = response.TestResult;
                result.TestLastExceptionText  = response.LastException;
                result.CanAccessCabFolder     = response.IsCabFolderAccessible;
                result.CabFolderExceptionText = response.CabFolderAccessLastException;

                // contine if the test succeeded and we're not just testing
                if ((result.TestStatus == StackHashErrorIndexDatabaseStatus.Success) &&
                    (result.CanAccessCabFolder) &&
                    (!arg.TestConnectionOnly))
                {
                    // make sure NETWORK SERVICE can access the cab folder
                    FolderPermissionHelper.NSAddAccess(arg.CabFolder);

                    if (arg.CreateDatabase)
                    {
                        installer = new InstallerInterface(arg.MasterConnectionString,
                                                           arg.ProfileName,
                                                           arg.CabFolder);

                        installer.Connect();

                        if (!installer.DatabaseExists())
                        {
                            installer.CreateDatabase(arg.UseDefaultLocation);
                        }
                    }

                    DBConfigSettings.Settings.ProfileName      = arg.ProfileName;
                    DBConfigSettings.Settings.ConnectionString = arg.ConnectionString;
                    DBConfigSettings.Settings.ProfileFolder    = arg.CabFolder;
                    DBConfigSettings.Settings.Save();

                    _configurationSucceeded = true;
                }
            }
            catch (Exception ex)
            {
                result.WorkerException = ex;
            }
            finally
            {
                if (installer != null)
                {
                    installer.Disconnect();
                }
            }

            // if a test exception was reported include it in the  WorkerException
            if (!string.IsNullOrEmpty(result.TestLastExceptionText))
            {
                if (result.WorkerException == null)
                {
                    result.WorkerException = new AdminReportException(result.TestLastExceptionText);
                }
                else
                {
                    result.WorkerException = new AdminReportException(result.TestLastExceptionText, result.WorkerException);
                }
            }
            else if (!string.IsNullOrEmpty(result.CabFolderExceptionText))
            {
                if (result.WorkerException == null)
                {
                    result.WorkerException = new AdminReportException(result.CabFolderExceptionText);
                }
                else
                {
                    result.WorkerException = new AdminReportException(result.CabFolderExceptionText, result.WorkerException);
                }
            }

            e.Result = result;
        }