C# Class GSF.Web.Hosting.EmbeddedResourcePathProvider

A System.Web.Hosting.VirtualPathProvider that allows serving pages from embedded resources.

ASP.NET retrieves files to serve via the System.Web.Hosting.HostingEnvironment. Rather than opening a file via System.IO.File, you ask the System.Web.Hosting.HostingEnvironment for its System.Web.Hosting.HostingEnvironment.VirtualPathProvider and ask that provider for the file. The provider will return a System.Web.Hosting.VirtualFile reference that will allow you to open a stream on the file and use the contents.

This implementation of System.Web.Hosting.VirtualPathProvider allows you to serve files to ASP.NET through embedded resources. Rather than deploying your web forms, user controls, etc., to the file system, you can embed the files as resources right in your assembly and deploy just your assembly. The System.Web.Hosting.VirtualPathProvider mechanism will take care of the rest.

Most System.Web.Hosting.VirtualPathProvider implementations handle both directories and files. This implementation handles only files. As such, if the System.Web.Hosting.VirtualPathProvider is used to enumerate available files (as in directory browsing), files provided via embedded resource will not be included.

To use this System.Web.Hosting.VirtualPathProvider, you need to do four things to your web application.

First, you need to set all of the files you want to serve from your assembly as embedded resources. By default, web forms and so forth are set as "content" files; setting them as embedded resources will package them into your assembly.

Second, in your AssemblyInfo.cs file (or whichever file you are declaring your assembly attributes in) you need to add one GSF.Web.Hosting.EmbeddedResourceFileAttribute for every file you plan on serving. This lets the provider know which embedded resources are available and which are actually resources for other purposes. Your assembly attributes will look something like this:

[assembly: EmbeddedResourceFileAttribute("MyNamespace.WebForm1.aspx", "MyNamespace")] [assembly: EmbeddedResourceFileAttribute("MyNamespace.UserControl1.ascx", "MyNamespace")]

Third, you need to register this provider at application startup so ASP.NET knows to use it. In your Global.asax, during Application_OnStart, put the following:

System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedResourcePathProvider());

Fourth, in your web.config file, you need to set up a configuration section called embeddedFileAssemblies that lets the provider know which assemblies should be queried for embedded files. A sample configuration section looks like this:

<configuration> <configSections> <section name="embeddedFileAssemblies" type="GSF.Configuration.StringCollectionSectionHandler, GSF.Web.Hosting.EmbeddedResourcePathProvider"/> </configSections> <embeddedFileAssemblies> <add value="My.Web.Assembly"/> </embeddedFileAssemblies> <!-- ... other web.config items ... --> </configuration>

Once you have that set up, you're ready to serve files from embedded resources. Simply deploy your application without putting the embedded resource files into the filesystem. When you visit the embedded locations, the provider will automatically retrieve the proper embedded resource.

File paths are mapped into the application using the GSF.Web.Hosting.EmbeddedResourceFileAttribute declarations and the GSF.Web.Hosting.EmbeddedResourcePathProvider.MapResourceToWebApplication method. This allows you to set up your web application as normal in Visual Studio and the folder structure, which automatically generates namespaces for your embedded resources, will translate into virtual folders in the embedded resource "filesystem."

By default, files that are embedded as resources will take precedence over files in the filesystem. If you would like the files in the filesystem to take precedence (that is, if you would like to allow the filesystem to "override" embedded files), you can set a key in the appSettings section of your web.config file that enables overrides:

<configuration> <!-- ... other web.config items ... --> <appSettings> <add key="GSF.Web.Hosting.EmbeddedResourcePathProvider.AllowOverrides" value="true"/> </appSettings> </configuration>

For more information on virtual filesystems in ASP.NET, check out System.Web.Hosting.VirtualPathProvider.

Inheritance: System.Web.Hosting.VirtualPathProvider
Exibir arquivo Open project: GridProtectionAlliance/gsf

Public Methods

Method Description
FileExists ( string virtualPath ) : bool

Gets a value that indicates whether a file exists in the virtual file system.

This override checks to see if the embedded resource file exists in memory. If so, this method will return . If not, it returns the value from the System.Web.Hosting.VirtualPathProvider.Previous virtual path provider.

GetCacheDependency ( string virtualPath, IEnumerable virtualPathDependencies, System.DateTime utcStart ) : System.Web.Caching.CacheDependency

Creates a cache dependency based on the specified virtual paths.

GetFile ( string virtualPath ) : System.Web.Hosting.VirtualFile

Gets a virtual file from the virtual file system.

Protected Methods

Method Description
GetConfiguredAssemblyNames ( ) : string[]

Gets the names of the configured assemblies from configuration.

Initialize ( ) : void

Initializes the System.Web.Hosting.VirtualPathProvider instance.

The set of assemblies configured to provide embedded resource files are queried for GSF.Web.Hosting.EmbeddedResourceFileAttribute attributes. For each one present, the associated embedded resource is added to the virtual file system served by this provider.

MapResourceToWebApplication ( string baseNamespace, string resourcePath ) : string

Maps an embedded resource ID into a web application relative path (~/path).

The baseNamespace is stripped from the front of the resourcePath and all but the last period in the remaining resourcePath is replaced with the directory separator character ('/'). Finally, that path is mapped into a web application relative path.

The filename being mapped must have an extension associated with it, and that extension may not have a period in it. Only one period will be kept in the mapped filename - others will be assumed to be directory separators. If a filename has multiple extensions (i.e., My.Custom.config), it will not map properly - it will end up being ~/My/Custom.config.

If baseNamespace does not occur at the start of the resourcePath, an System.InvalidOperationException is thrown.

ProcessEmbeddedFiles ( string assemblyName ) : void

Reads in the embedded files from an assembly an processes them into the virtual file system.

The assemblyName will be passed to System.Reflection.Assembly.Load(string) so the associated assembly can be processed. If the assembly is not found, a System.IO.FileNotFoundException is thrown.

Once the assembly is retrieved, it is queried for GSF.Web.Hosting.EmbeddedResourceFileAttribute instances. For each one found, the associated resources are processed into virtual files that will be stored in GSF.Web.Hosting.EmbeddedResourcePathProvider.Files for later use.

Private Methods

Method Description
FileHandledByBaseProvider ( string absolutePath ) : bool

Determines if a file should be handled by the base provider or if it should be handled by this provider.

ValidateResourcePath ( string paramName, string path ) : void

Validates an embedded resource path or namespace.

Method Details

FileExists() public method

Gets a value that indicates whether a file exists in the virtual file system.

This override checks to see if the embedded resource file exists in memory. If so, this method will return . If not, it returns the value from the System.Web.Hosting.VirtualPathProvider.Previous virtual path provider.

/// Thrown if is . /// /// Thrown if is . ///
public FileExists ( string virtualPath ) : bool
virtualPath string The path to the virtual file.
return bool

GetCacheDependency() public method

Creates a cache dependency based on the specified virtual paths.
/// Thrown if is . /// /// Thrown if is . ///
public GetCacheDependency ( string virtualPath, IEnumerable virtualPathDependencies, System.DateTime utcStart ) : System.Web.Caching.CacheDependency
virtualPath string The path to the primary virtual resource.
virtualPathDependencies IEnumerable An array of paths to other resources required by the primary virtual resource.
utcStart System.DateTime The UTC time at which the virtual resources were read.
return System.Web.Caching.CacheDependency

GetConfiguredAssemblyNames() protected static method

Gets the names of the configured assemblies from configuration.
protected static GetConfiguredAssemblyNames ( ) : string[]
return string[]

GetFile() public method

Gets a virtual file from the virtual file system.
/// Thrown if is . /// /// Thrown if is . ///
public GetFile ( string virtualPath ) : System.Web.Hosting.VirtualFile
virtualPath string The path to the virtual file.
return System.Web.Hosting.VirtualFile

Initialize() protected method

Initializes the System.Web.Hosting.VirtualPathProvider instance.

The set of assemblies configured to provide embedded resource files are queried for GSF.Web.Hosting.EmbeddedResourceFileAttribute attributes. For each one present, the associated embedded resource is added to the virtual file system served by this provider.

protected Initialize ( ) : void
return void

MapResourceToWebApplication() protected static method

Maps an embedded resource ID into a web application relative path (~/path).

The baseNamespace is stripped from the front of the resourcePath and all but the last period in the remaining resourcePath is replaced with the directory separator character ('/'). Finally, that path is mapped into a web application relative path.

The filename being mapped must have an extension associated with it, and that extension may not have a period in it. Only one period will be kept in the mapped filename - others will be assumed to be directory separators. If a filename has multiple extensions (i.e., My.Custom.config), it will not map properly - it will end up being ~/My/Custom.config.

If baseNamespace does not occur at the start of the resourcePath, an System.InvalidOperationException is thrown.

/// Thrown if or is . /// /// Thrown if or : /// /// /// /// Is . /// /// /// /// /// Start or end with period. /// /// /// /// /// Contain two or more periods together (like MyNamespace..MySubnamespace). /// /// /// ///
protected static MapResourceToWebApplication ( string baseNamespace, string resourcePath ) : string
baseNamespace string /// The base namespace of the resource to map. ///
resourcePath string /// The fully qualified embedded resource path to map. ///
return string

ProcessEmbeddedFiles() protected method

Reads in the embedded files from an assembly an processes them into the virtual file system.

The assemblyName will be passed to System.Reflection.Assembly.Load(string) so the associated assembly can be processed. If the assembly is not found, a System.IO.FileNotFoundException is thrown.

Once the assembly is retrieved, it is queried for GSF.Web.Hosting.EmbeddedResourceFileAttribute instances. For each one found, the associated resources are processed into virtual files that will be stored in GSF.Web.Hosting.EmbeddedResourcePathProvider.Files for later use.

/// Thrown if is . /// /// Thrown if is . /// /// Thrown if the indicated by /// is not found. ///
protected ProcessEmbeddedFiles ( string assemblyName ) : void
assemblyName string The name of the to load and process.
return void