using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Reflection; using System.Diagnostics; namespace HLWebPluginHost.PluginLib { /// /// The purpose of this class is to actually provide the bytes for a given resource. /// We subclass VirtualFile and override Open() to read the requested resource out of our plugin assembly. /// public class AssemblyResourceVirtualFile : System.Web.Hosting.VirtualFile { private string path; /// /// Constructor /// /// public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath) { path = VirtualPathUtility.ToAppRelative(virtualPath); } public override Stream Open() { string[] parts = path.Split('/'); string assemblyName = parts[2]; string resourceName = parts[3]; // While it is possible to directly load an assembly using Assembly.LoadFile() or Assembly.LoadFrom(), these methods will lock the DLL // file for the duration of the process (e.g. until you restart IIS). A much better way to load an assembly is to create an in-memory copy // of the DLL and load that, which is what we are doing here: assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName); byte[] assemblyBytes = File.ReadAllBytes(assemblyName); Assembly assembly = Assembly.Load(assemblyBytes); if (assembly != null) return assembly.GetManifestResourceStream(resourceName); return null; } } }