WriteResourceToStream() public static method

public static WriteResourceToStream ( Stream outputStream, Type type, string resourceName ) : void
outputStream Stream
type System.Type
resourceName string
return void
Ejemplo n.º 1
0
        public static Action <HttpContextBase> Create(IEnumerable <string> resourceNames, string mediaType, Encoding responseEncoding, bool cacheResponse)
        {
            Debug.Assert(resourceNames != null);
            Debug.AssertStringNotEmpty(mediaType);

            return(context =>
            {
                //
                // Set the response headers for indicating the content type
                // and encoding (if specified).
                //

                var response = context.Response;
                response.ContentType = mediaType;

                if (cacheResponse)
                {
                    response.Cache.SetCacheability(HttpCacheability.Public);
                    response.Cache.SetExpires(DateTime.MaxValue);
                }

                if (responseEncoding != null)
                {
                    response.ContentEncoding = responseEncoding;
                }

                foreach (var resourceName in resourceNames)
                {
                    ManifestResourceHelper.WriteResourceToStream(response.OutputStream, typeof(ManifestResourceHandler), resourceName);
                }
            });
        }
Ejemplo n.º 2
0
        private static string CalculateHash()
        {
            var memoryStream = new MemoryStream();

            foreach (var resourceName in _styleSheetResourceNames)
            {
                ManifestResourceHelper.WriteResourceToStream(memoryStream, typeof(StyleSheetHelper), resourceName);
            }

            return(MD5.Create()
                   .ComputeHash(memoryStream)
                   .Select(b => b.ToString("x2"))
                   .ToDelimitedString(string.Empty));
        }
        public void ProcessRequest(HttpContext context)
        {
            //
            // Set the response headers for indicating the content type
            // and encoding (if specified).
            //

            HttpResponse response = context.Response;

            response.ContentType = _contentType;

            if (_responseEncoding != null)
            {
                response.ContentEncoding = _responseEncoding;
            }

            ManifestResourceHelper.WriteResourceToStream(response.OutputStream, _resourceName);
        }
Ejemplo n.º 4
0
        private void InitializeDatabase()
        {
            string connectionString = ConnectionString;

            Debug.AssertStringNotEmpty(connectionString);

            string 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.
                //

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

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

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

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

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

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

                        TimeSpan 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);
                }
            }
        }