Getting Started Series - Virtual File System

It does not exist in the file system (disk) virtual file system makes it possible to manage physical on file. It is mainly used to embed (js, css, image, cshtml ...) file to the assembly, and will run they like to use the same physical file.

Volo.Abp.VirtualFileSystem nuggets 包

Volo.Abp.VirtualFileSystem is the core package of the virtual file system using the Package Manager Console (PMC) and install it to your project:

Install-Package Volo.Abp.VirtualFileSystem

Start the default template already installed this nuget package, so in most cases you do not need to install it manually.

Then you can add the module in  AbpVirtualFileSystemModule  dependency:

using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

namespace MyCompany.MyProject
{
    [DependsOn(typeof(AbpVirtualFileSystemModule))]
    public class MyModule : AbpModule
    {
        //...
    }
}

Registration embedded file

To embed a file into an assembly, you first need to mark the file as an embedded resource. The easiest way is in the  Solution Explorer  select the file, and then find the  "Properties"  window  "Build Action"  is set to  "Embedded resources " such as:

If you need to add multiple files, this will be very tedious Alternatively, you can directly edit.  .Csproj  file:

<ItemGroup>
  <EmbeddedResource Include="MyResources\**\*.*" />
  <Content Remove="MyResources\**\*.*" />
</ItemGroup>

This configuration add an item recursively  MyResources  all files folder (including future new files added).

You'll then need  AbpVirtualFileSystemOptions to configure the module to register the embedded files to the virtual file system such as:

using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

namespace MyCompany.MyProject
{
    [DependsOn(typeof(AbpVirtualFileSystemModule))]
    public class MyModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                //Register all embedded files of this assembly to the virtual file system
                options.FileSets.AddEmbedded<MyModule>("YourRootNameSpace");
            });

            //...
        }
    }
}

AddEmbedded Extension method requires a class, find all embedded files from a given class assembly, and register them to the virtual file system it also has a more concise wording:

options.FileSets.Add(new EmbeddedFileSet(typeof(MyModule).Assembly), "YourRootNameSpace");

"YourRootNameSpace" is the root namespace name of the project. If your project's root namespace name is blank, you do not pass this parameter.

Gets the virtual file: IVirtualFileProvider

After the file is embedded in the assembly and register to the virtual file system that can be used IVirtualFileProviderto get the file or directory contents:

public class MyService
{
    private readonly IVirtualFileProvider _virtualFileProvider;

    public MyService(IVirtualFileProvider virtualFileProvider)
    {
        _virtualFileProvider = virtualFileProvider;
    }

    public void Foo()
    {
        //Getting a single file
        var file = _virtualFileProvider.GetFileInfo("/MyResources/js/test.js");
        var fileContent = file.ReadAsString(); //ReadAsString is an extension method of ABP

        //Getting all files/directories under a directory
        var directoryContents = _virtualFileProvider.GetDirectoryContents("/MyResources/js");
    }
}

Embedded processing files during development

The embedded files to the module assembly and can, however, which allows developers to reference an assembly module (or add nuget package) use it to create reusable modules very valuable in another project. Itself becomes a bit difficult.

Suppose you are developing a module that contains an embedded JavaScript file. When you change a file, you must recompile the project, restart the application and refresh the browser page for the changes to take effect. Obviously, this is very time consuming and tedious.

What we need is the ability to use physical file an application directly in the development, so that any changes to the JavaScript file synchronization when the browser refresh.  ReplaceEmbeddedByPhysical The method makes it possible.

The following example shows the application depends on the module containing the embedded files ( "MyModule"), and the application may be used directly in the source code modules of the development process.

[DependsOn(typeof(MyModule))]
public class MyWebAppModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var hostingEnvironment = context.Services.GetHostingEnvironment();

        if (hostingEnvironment.IsDevelopment()) //only for development time
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                //ReplaceEmbeddedByPhysical gets the root folder of the MyModule project
                options.FileSets.ReplaceEmbeddedByPhysical<MyModule>(
                    Path.Combine(hostingEnvironment.ContentRootPath, "..\\MyModuleProject")
                );
            });
        }

        //...
    }
}

The code above assumptions MyWebAppModuleand MyModulea Visual Studio solution in two different projects,  MyWebAppModulerelies on MyModule.

ASP.NET Core Integration

The virtual file system seamlessly integrate with ASP.NET Core:

  • Virtual File can be used like a physical on Web application (static) file the same.
  • Razor Views, Razor Pages, js, css, image files, and all other Web content can be embedded in the assembly and use of the same as the physical file.
  • Like file an application (or other module) can cover modules virtual file, just like with the same name and extension into a virtual file in the same folder.

Virtual File Middleware

Middleware is used to provide the embedded virtual file (js, css, image ...) file to the client / browser, like  wwwroot  as folder physical (static) file to add it after the static file middleware, As follows:

app.UseVirtualFiles();

After adding virtual file middleware middleware static file, such that the physical file is made possible by placing a virtual file in the same location, so as to cover the virtual file physical file.

Virtual file middleware virtual content wwwroot folder - like static files.

Views & Pages

No configuration to use embedded razor Views / pages in your application. They only need to be placed in the module to be developed in the Standard Views / Pages virtual folder.

If the module / application will place the new files in the same location, it overwrites the embedded Views / Pages.

 

Published 87 original articles · won praise 69 · Views 600,000 +

Guess you like

Origin blog.csdn.net/S2T11Enterprise/article/details/104530274