Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebPluginHost/PluginLib/AssemblyResourceVirtualFile.cs @ 4606

Last change on this file since 4606 was 4604, checked in by dkahn, 14 years ago

#1198 Imported new Plugin Host solution for the new MVC2 based web application

File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.IO;
6using System.Reflection;
7using System.Diagnostics;
8
9namespace HLWebPluginHost.PluginLib {
10
11  /// <summary>
12  /// The purpose of this class is to actually provide the bytes for a given resource.
13  /// We subclass VirtualFile and override Open() to read the requested resource out of our plugin assembly.
14  /// </summary>
15  public class AssemblyResourceVirtualFile : System.Web.Hosting.VirtualFile {
16    private string path;
17
18    /// <summary>
19    /// Constructor
20    /// </summary>
21    /// <param name="virtualPath"></param>
22    public AssemblyResourceVirtualFile(string virtualPath)
23      : base(virtualPath) {
24      path = VirtualPathUtility.ToAppRelative(virtualPath);
25    }
26
27    public override Stream Open() {
28      string[] parts = path.Split('/');
29      string assemblyName = parts[2];
30      string resourceName = parts[3];
31
32      // While it is possible to directly load an assembly using Assembly.LoadFile() or Assembly.LoadFrom(), these methods will lock the DLL
33      // 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
34      // of the DLL and load that, which is what we are doing here:
35      assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
36      byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
37      Assembly assembly = Assembly.Load(assemblyBytes);
38
39      if (assembly != null)
40        return assembly.GetManifestResourceStream(resourceName);
41
42      return null;
43    }
44  }
45
46}
Note: See TracBrowser for help on using the repository browser.