コード例 #1
0
        public virtual HttpResponseMessage HelpPageCss(string cssFileName)
        {
            if (!String.IsNullOrEmpty(cssFileName))
            {
                var cssResourcePath = string.Format("WebApi.HelpPage.Views.Content.{0}.css", cssFileName);
                if (ManifestResourceHelper.checkExistsManifestResource(cssResourcePath))
                {
                    var cssContent = ManifestResourceHelper.getManifestResourceString(cssResourcePath);
                    return(new HttpResponseMessage
                    {
                        Content = new StringContent(cssContent, System.Text.Encoding.UTF8, "text/css")
                    });
                }
            }

            return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Css not found."));
        }
コード例 #2
0
        private void InitializeDatabase()
        {
            var connectionString = ConnectionString;

            Debug.Assert(!string.IsNullOrEmpty(connectionString));

            var dbFilePath = ConnectionStringHelper.GetDataSourceFilePath(connectionString);

            if (File.Exists(dbFilePath))
            {
                return;
            }

            //
            // Make sure that we don't have multiple instances trying to create the database.
            //

            lock (_mdbInitializationLock)
            {
                //
                // Just double-check that no other thread has created the database while
                // we were waiting for the lock.
                //

                if (File.Exists(dbFilePath))
                {
                    return;
                }

                //
                // Create a temporary copy of the mkmdb.vbs script.
                // We do this in the same directory as the resulting database for security permission purposes.
                //

                var scriptPath = Path.Combine(Path.GetDirectoryName(dbFilePath), _scriptResourceName);

                using (var scriptStream = new FileStream(scriptPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    ManifestResourceHelper.WriteResourceToStream(scriptStream, GetType(), _scriptResourceName);
                }

                //
                // Run the script file to create the database using batch
                // mode (//B), which suppresses script errors and prompts
                // from displaying.
                //

                var psi = new ProcessStartInfo(
                    "cscript", "\"" + scriptPath + "\" \"" + dbFilePath + "\" //B //NoLogo");

                psi.UseShellExecute = false;    // i.e. CreateProcess
                psi.CreateNoWindow  = true;     // Stay lean, stay mean

                try
                {
                    using (var process = Process.Start(psi))
                    {
                        //
                        // A few seconds should be plenty of time to create the database.
                        //

                        var tolerance = TimeSpan.FromSeconds(2);
                        if (!process.WaitForExit((int)tolerance.TotalMilliseconds))
                        {
                            //
                            // but it wasn't, so clean up and throw an exception!
                            // Realistically, I don't expect to ever get here!
                            //

                            process.Kill();

                            throw new Exception(string.Format(
                                                    "The Microsoft Access database creation script took longer than the allocated time of {0} seconds to execute. "
                                                    + "The script was terminated prematurely.",
                                                    tolerance.TotalSeconds));
                        }

                        if (process.ExitCode != 0)
                        {
                            throw new Exception(string.Format(
                                                    "The Microsoft Access database creation script failed with exit code {0}.",
                                                    process.ExitCode));
                        }
                    }
                }
                finally
                {
                    //
                    // Clean up after ourselves!!
                    //

                    File.Delete(scriptPath);
                }
            }
        }