예제 #1
0
        /// <summary>
        /// Starts the instance.
        /// </summary>
        /// <param name="instanceName">Name of the instance.</param>
        /// <exception cref="Exception">
        /// There is an instance already running.
        /// or
        /// </exception>
        public void StartInstance(string instanceName, int port)
        {
            int instanceIndex = -1;

            if (iisExpressProcess != null)
            {
                throw new Exception("There is an instance already running.");
            }

            Dispatcher.Invoke(() =>
            {
                txtStatus.Text  = "Starting...";
                txtConsole.Text = string.Empty;

                var items     = (List <string>)cbInstances.ItemsSource;
                instanceIndex = items.IndexOf(instanceName);
                if (instanceIndex != -1)
                {
                    cbInstances.SelectedIndex = instanceIndex;
                }

                txtPort.Text = port.ToString();
            });

            //
            // Find the path to the RockWeb instance.
            //
            var path = Path.Combine(Support.GetInstancesPath(), instanceName, "RockWeb");

            if (!Directory.Exists(path) || instanceIndex == -1)
            {
                throw new Exception(string.Format("The instance '{0}' was not found.", instanceName));
            }

            //
            // Check if the Database file already exists and if not create the
            // Run.Migration file so Rock initializes itself.
            //
            var dbPath = Path.Combine(path, "App_Data", "Database.mdf");

            if (!File.Exists(dbPath))
            {
                var runMigrationPath = Path.Combine(path, "App_Data", "Run.Migration");
                File.WriteAllText(runMigrationPath, string.Empty);
            }

            ConfigureConnectionString(path);

            //
            // Prepare the IIS Express process for this RockWeb.
            //
            iisExpressProcess = new Utilities.ConsoleApp(GetIisExecutable());
            iisExpressProcess.ProcessExited        += IisExpressProcess_Exited;
            iisExpressProcess.StandardTextReceived += IisExpressProcess_StandardTextReceived;
            RunningInstanceName = instanceName;

            //
            // Update the status text to contain a clickable link to the instance.
            //
            Dispatcher.Invoke(() =>
            {
                var linkText = string.Format("http://localhost:{0}/", port);
                var link     = new Hyperlink(new Run(linkText))
                {
                    NavigateUri = new Uri(linkText)
                };
                link.RequestNavigate += StatusLink_RequestNavigate;
                txtStatus.Inlines.Clear();
                txtStatus.Inlines.Add(new Run("Running at "));
                txtStatus.Inlines.Add(link);

                UpdateState();
            });

            //
            // Launch IIS Express.
            //
            iisExpressProcess.ExecuteAsync(String.Format("/path:{0}", path), String.Format("/port:{0}", port));
        }