How does .NET5 embed static files in DLLs, and how to use the files embedded in DLLs with the host program?

TinyVFS is a virtual file system, inspired by the ABP vNext framework. It can embed js, CSS, image, cshtml, and other files into the assembly, and they can be used like physical files at runtime.

Features

Quick Start

Install-Package
Install-Package TinyVFS
Registering Embedded Files
Edit web resource project .csproj
  1. <ItemGroup>
  2. <EmbeddedResource Include="MyResources\**\*.*" />
  3. <Content Remove="MyResources\**\*.*" />
  4. </ItemGroup>
Embed the file into the virtual file system with the following code snippet,
  1. services.AddVirtualFilesService();
  2. services.Configure < VirtualFileSystemOptions > (options => {
  3. options.FileSets.AddEmbedded < WebApplication1.Pages.IndexModel > ("WebResources");
  4. });
Getting Virtual Files: IVirtualFileProvider
After embedding into the assembly, you can obtain the file or directory content through IVirtualFileProvider
  1. public class MyService {
  2. private readonly IVirtualFileProvider _virtualFileProvider;
  3. public MyService(IVirtualFileProvider virtualFileProvider) {
  4. _virtualFileProvider = virtualFileProvider;
  5. }
  6. public void Foo() {
  7. //Getting a single file
  8. var file = _virtualFileProvider.GetFileInfo("/MyResources/js/test.js");
  9. var fileContent = file.ReadAsString(); //ReadAsString is an extension method of ABP
  10. //Getting all files/directories under a directory
  11. var directoryContents = _virtualFileProvider.GetDirectoryContents("/MyResources/js");
  12. }
  13. }
Dynamic listening file
When we are developing on the local machine, maybe we will modify the static files in the resource project, so the normal operation can let us regenerate the code.
Now we can use ReplaceEmbeddedByPhysical to refresh through the browser to get the latest file information,
  1. services.AddVirtualFilesService();
  2. services.Configure < VirtualFileSystemOptions > (options => {
  3. options.FileSets.ReplaceEmbeddedByPhysical < WebApplication1.Pages.IndexModel > (Path.Combine(WebHostEnvironment.ContentRootPath, "..\\WebResources"));
  4. });
Virtual Files Middleware
Virtual file middleware is used to provide embedded (js, CSS, image …) files to the client/browser, just like the physical (static) file in the wwwroot folder. Add it after the static file middleware, as shown below,
  1. app.UseVirtualFiles();
If you want to extend other file formats, you can use the overloaded method, as shown below,
  1. var provider = new FileExtensionContentTypeProvider();
  2. provider.Mappings[".less"] = "text/css";
  3. app.UseVirtualFiles(provider);
By setting the virtual file middleware, it is possible to place the physical file in the same position as the virtual file, thereby making it possible for the physical file to cover the virtual file.
ASP.NET Core Integration
Virtual files can be integrated directly into ASP.NET Core,
Views & Pages
They do not require any configuration and can be used in our applications. When these files exist in our physical directory, they overwrite the virtual files.