Improved Virtual-File-System.md and minor enhancements

pull/279/head
Halil İbrahim Kalkan 8 years ago
parent a244e8562c
commit 24c81d96bd

@ -7,7 +7,7 @@
* <a href="Getting-Started-AspNetCore-Application.md" target="_blank">With ASP.NET Core Web Application</a>
* Fundamentals
* <a href="Dependency-Injection.md" target="_blank">Dependency Injection</a>
* Virtual File System
* [Virtual File System](Virtual-File-System.md)
* Localization
* Exception Handling
* [Multi Tenancy](Multi-Tenancy.md)

@ -30,9 +30,21 @@ namespace MyCompany.MyProject
#### Registering Embedded Files
A file should be first marked as embedded resource to embed the file into the assembly. The easiest way to do it is to select the file from the **Solution Explorer** and set **Build Action** to **Embedded Resource** from the **Properties** window. Example:
![build-action-embedded-resource-sample](images/build-action-embedded-resource-sample.png)
A file should be first registered in order to use it in the application. Example:
If you want to add multiple files, this can be tedious. Alternatively, you can directly edit your **.csproj** file:
````C#
<ItemGroup>
<None Remove="MyResources\**\*.*" />
</ItemGroup>
````
This configuration recursively adds all files under the **MyResources** folder of the project (including the files you will add in the future).
Then the module should configure `VirtualFileSystemOptions` to register embedded files to the virtual file system. Example:
````C#
using Microsoft.Extensions.DependencyInjection;
@ -58,6 +70,38 @@ namespace MyCompany.MyProject
}
````
AddEmbedded extension method takes a
`AddEmbedded` extension method takes a class, finds all embedded files from the assembly of the given class and register to the virtual file system. It's a shortcut and could be written as shown below:
````C#
options.FileSets.Add(new EmbeddedFileSet(typeof(MyModule).Assembly));
````
#### Getting Virtual Files: IVirtualFileProvider
After embedding a file into an assembly and registering to the virtual file system, `IVirtualFileProvider` can be used to get files or directory contents:
````C#
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");
}
}
````
s

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,31 @@
using System.IO;
using System.Text;
using JetBrains.Annotations;
using Volo.Abp;
namespace Microsoft.Extensions.FileProviders
{
public static class AbpFileInfoExtensions
{
/// <summary>
/// Reads file content as string using <see cref="Encoding.UTF8"/> encoding.
/// </summary>
public static string ReadAsString([NotNull] this IFileInfo fileInfo)
{
return fileInfo.ReadAsString(Encoding.UTF8);
}
/// <summary>
/// Reads file content as string using the given <paramref name="encoding"/>.
/// </summary>
public static string ReadAsString([NotNull] this IFileInfo fileInfo, Encoding encoding)
{
Check.NotNull(fileInfo, nameof(fileInfo));
using (var stream = fileInfo.CreateReadStream())
{
return encoding.GetString(stream.GetAllBytes());
}
}
}
}

@ -21,7 +21,7 @@ namespace Volo.Abp.VirtualFileSystem.Embedded
public EmbeddedFileSet(
[NotNull] Assembly assembly,
[CanBeNull] string baseNamespace,
[CanBeNull] string baseNamespace = null,
[CanBeNull] string baseFolderInProject = null)
{
Check.NotNull(assembly, nameof(assembly));

@ -12,7 +12,7 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Volo\Abp\VirtualFileSystem\MyResources\js\jquery-3-1-1-min.js" />
<EmbeddedResource Include="Volo\Abp\VirtualFileSystem\MyResources\js\*.js" />
</ItemGroup>
<ItemGroup>

@ -3,24 +3,25 @@ using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem.Embedded;
using Xunit;
namespace Volo.Abp.VirtualFileSystem
{
public class VirtualFileProvider_Tests : AbpIntegratedTest<VirtualFileProvider_Tests.TestModule>
{
private readonly IVirtualFileProvider _embeddedFileManager;
private readonly IVirtualFileProvider _virtualFileProvider;
public VirtualFileProvider_Tests()
{
_embeddedFileManager = ServiceProvider.GetRequiredService<IVirtualFileProvider>();
_virtualFileProvider = ServiceProvider.GetRequiredService<IVirtualFileProvider>();
}
[Fact]
public void Should_Define_And_Get_Embedded_Resources()
{
//Act
var resource = _embeddedFileManager.GetFileInfo("/js/jquery-3-1-1-min.js");
var resource = _virtualFileProvider.GetFileInfo("/js/jquery-3-1-1-min.js");
//Assert
resource.ShouldNotBeNull();

Loading…
Cancel
Save