Browse Source

Resolved #8006: Virtual File Middleware should be compatible with IWebHostEnvironment.WebRootFileProvider

pull/8074/head
Halil İbrahim Kalkan 6 years ago
parent
commit
73e700addc
  1. 7
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  2. 13
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs
  3. 5
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs
  4. 6
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Styles/StyleBundler.cs
  5. 7
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs
  6. 2
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs
  7. 2
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs
  8. 16
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/VirtualFileSystemApplicationBuilderExtensions.cs
  9. 18
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs
  10. 17
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/VirtualFileSystem/WebContentFileProvider.cs
  11. 4
      framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs
  12. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs
  13. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/AbpAspNetCoreMvcUiBootstrapDemoModule.cs
  14. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo/AbpAspNetCoreMvcUiThemeBasicDemoModule.cs
  15. 2
      framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions_Tests.cs
  16. 2
      framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestModule.cs

7
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs

@ -24,7 +24,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected readonly AbpBundlingOptions Options;
protected readonly AbpBundleContributorOptions ContributorOptions;
protected readonly IWebContentFileProvider WebContentFileProvider;
protected readonly IWebHostEnvironment HostingEnvironment;
protected readonly IScriptBundler ScriptBundler;
protected readonly IStyleBundler StyleBundler;
@ -42,7 +41,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
IServiceProvider serviceProvider,
IDynamicFileProvider dynamicFileProvider,
IBundleCache bundleCache,
IWebContentFileProvider webContentFileProvider,
IWebRequestResources requestResources)
{
Options = options.Value;
@ -52,7 +50,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
ServiceProvider = serviceProvider;
DynamicFileProvider = dynamicFileProvider;
BundleCache = bundleCache;
WebContentFileProvider = webContentFileProvider;
RequestResources = requestResources;
StyleBundler = styleBundler;
@ -117,7 +114,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
foreach (var file in files)
{
var watchDisposeHandle = WebContentFileProvider.Watch(file).RegisterChangeCallback(_ =>
var watchDisposeHandle = HostingEnvironment.WebRootFileProvider.Watch(file).RegisterChangeCallback(_ =>
{
lock (cacheValue.WatchDisposeHandles)
{
@ -215,7 +212,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual BundleConfigurationContext CreateBundleConfigurationContext()
{
return new BundleConfigurationContext(ServiceProvider, WebContentFileProvider);
return new BundleConfigurationContext(ServiceProvider, HostingEnvironment.WebRootFileProvider);
}
protected virtual List<IBundleContributor> GetContributors(BundleConfigurationCollection bundles, string bundleName)

13
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs

@ -1,5 +1,6 @@
using System;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -15,12 +16,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public ILogger<BundlerBase> Logger { get; set; }
protected IWebContentFileProvider WebContentFileProvider { get; }
protected IWebHostEnvironment HostEnvironment { get; }
protected IMinifier Minifier { get; }
protected BundlerBase(IWebContentFileProvider webContentFileProvider, IMinifier minifier)
protected BundlerBase(IWebHostEnvironment hostEnvironment, IMinifier minifier)
{
WebContentFileProvider = webContentFileProvider;
HostEnvironment = hostEnvironment;
Minifier = minifier;
Logger = NullLogger<BundlerBase>.Instance;
@ -100,11 +101,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual IFileInfo GetFileInfo(IBundlerContext context, string file)
{
var fileInfo = WebContentFileProvider.GetFileInfo(file);
var fileInfo = HostEnvironment.WebRootFileProvider.GetFileInfo(file);
if (!fileInfo.Exists)
{
throw new AbpException($"Could not find file '{file}' using {nameof(IWebContentFileProvider)}");
throw new AbpException($"Could not find file '{file}'");
}
return fileInfo;
@ -127,7 +128,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
foreach (var suffix in _minFileSuffixes)
{
var fileInfo = WebContentFileProvider.GetFileInfo(
var fileInfo = HostEnvironment.WebRootFileProvider.GetFileInfo(
$"{file.RemovePostFix($".{FileExtension}")}.{suffix}.{FileExtension}"
);

5
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs

@ -1,4 +1,5 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.Minify.Scripts;
@ -8,8 +9,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts
{
public override string FileExtension => "js";
public ScriptBundler(IWebContentFileProvider webContentFileProvider, IJavascriptMinifier minifier)
: base(webContentFileProvider, minifier)
public ScriptBundler(IWebHostEnvironment hostEnvironment, IJavascriptMinifier minifier)
: base(hostEnvironment, minifier)
{
}

6
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Styles/StyleBundler.cs

@ -11,10 +11,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.Styles
private readonly IWebHostEnvironment _hostingEnvironment;
public override string FileExtension => "css";
public StyleBundler(IWebContentFileProvider webContentFileProvider, ICssMinifier minifier, IWebHostEnvironment hostingEnvironment)
: base(webContentFileProvider, minifier)
public StyleBundler(IWebHostEnvironment hostEnvironment, ICssMinifier minifier)
: base(hostEnvironment, minifier)
{
_hostingEnvironment = hostingEnvironment;
_hostingEnvironment = hostEnvironment;
}
public string GetAbsolutePath(string relativePath)

7
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs

@ -19,18 +19,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public ILogger<AbpTagHelperResourceService> Logger { get; set; }
protected IBundleManager BundleManager { get; }
protected IWebContentFileProvider WebContentFileProvider { get; }
protected IWebHostEnvironment HostingEnvironment { get; }
protected readonly AbpBundlingOptions Options;
protected AbpTagHelperResourceService(
IBundleManager bundleManager,
IWebContentFileProvider webContentFileProvider,
IOptions<AbpBundlingOptions> options,
IWebHostEnvironment hostingEnvironment)
{
BundleManager = bundleManager;
WebContentFileProvider = webContentFileProvider;
HostingEnvironment = hostingEnvironment;
Options = options.Value;
@ -66,11 +63,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
foreach (var bundleFile in bundleFiles)
{
var file = WebContentFileProvider.GetFileInfo(bundleFile);
var file = HostingEnvironment.WebRootFileProvider.GetFileInfo(bundleFile);
if (file == null || !file.Exists)
{
throw new AbpException($"Could not find the bundle file '{bundleFile}' from {nameof(IWebContentFileProvider)}");
throw new AbpException($"Could not find the bundle file '{bundleFile}'");
}
if (file.Length > 0)

2
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs

@ -18,12 +18,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public AbpTagHelperScriptService(
IBundleManager bundleManager,
IWebContentFileProvider webContentFileProvider,
IOptions<AbpBundlingOptions> options,
IWebHostEnvironment hostingEnvironment
) : base(
bundleManager,
webContentFileProvider,
options,
hostingEnvironment)
{

2
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs

@ -18,12 +18,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public AbpTagHelperStyleService(
IBundleManager bundleManager,
IWebContentFileProvider webContentFileProvider,
IOptions<AbpBundlingOptions> options,
IWebHostEnvironment hostingEnvironment
) : base(
bundleManager,
webContentFileProvider,
options,
hostingEnvironment)
{

16
framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/VirtualFileSystemApplicationBuilderExtensions.cs

@ -1,22 +1,20 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder
{
public static class VirtualFileSystemApplicationBuilderExtensions
{
[Obsolete("Use UseStaticFiles() instead. UseVirtualFiles is not needed anymore.")]
public static IApplicationBuilder UseVirtualFiles(this IApplicationBuilder app, Action<StaticFileOptions> configure = null)
{
var staticFileOptions = new StaticFileOptions
if (configure != null)
{
FileProvider = app.ApplicationServices.GetRequiredService<IWebContentFileProvider>(),
ContentTypeProvider = app.ApplicationServices.GetRequiredService<AbpFileExtensionContentTypeProvider>()
};
configure?.Invoke(staticFileOptions);
return app.UseStaticFiles(staticFileOptions);
configure(app.ApplicationServices.GetRequiredService<IOptions<StaticFileOptions>>().Value);
}
return app.UseStaticFiles();
}
}
}

18
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs

@ -1,9 +1,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.Auditing;
using Volo.Abp.Authorization;
using Volo.Abp.ExceptionHandling;
@ -35,9 +38,13 @@ namespace Volo.Abp.AspNetCore
options.Contributors.Add(new AspNetCoreAuditLogContributor());
});
Configure<StaticFileOptions>(options =>
{
options.ContentTypeProvider = context.Services.GetRequiredService<AbpFileExtensionContentTypeProvider>();
});
AddAspNetServices(context.Services);
context.Services.AddObjectAccessor<IApplicationBuilder>();
context.Services.AddAbpDynamicOptions<RequestLocalizationOptions, AbpRequestLocalizationOptionsManager>();
}
@ -45,5 +52,14 @@ namespace Volo.Abp.AspNetCore
{
services.AddHttpContextAccessor();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
context.GetEnvironment().WebRootFileProvider =
new CompositeFileProvider(
context.GetEnvironment().WebRootFileProvider,
context.ServiceProvider.GetRequiredService<IWebContentFileProvider>()
);
}
}
}

17
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/VirtualFileSystem/WebContentFileProvider.cs

@ -4,7 +4,6 @@ using System.Linq;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Volo.Abp.DependencyInjection;
@ -17,7 +16,7 @@ namespace Volo.Abp.AspNetCore.VirtualFileSystem
private readonly IVirtualFileProvider _virtualFileProvider;
private readonly IFileProvider _fileProvider;
private readonly IWebHostEnvironment _hostingEnvironment;
private string _rootPath = "/wwwroot"; //TODO: How to handle wwwroot naming?
private string _rootPath = "/wwwroot";
protected AbpAspNetCoreContentOptions Options { get; }
@ -93,22 +92,12 @@ namespace Volo.Abp.AspNetCore.VirtualFileSystem
protected virtual IFileProvider CreateFileProvider()
{
var fileProviders = new List<IFileProvider>()
var fileProviders = new List<IFileProvider>
{
new PhysicalFileProvider(_hostingEnvironment.ContentRootPath),
_virtualFileProvider
};
if (_hostingEnvironment.IsDevelopment() &&
_hostingEnvironment.WebRootFileProvider is CompositeFileProvider compositeFileProvider)
{
var staticWebAssetsFileProviders = compositeFileProvider
.FileProviders
.Where(f => f.GetType().Name.Equals("StaticWebAssetsFileProvider")).ToList();
fileProviders.AddRange(staticWebAssetsFileProviders);
}
return new CompositeFileProvider(
fileProviders
);
@ -124,4 +113,4 @@ namespace Volo.Abp.AspNetCore.VirtualFileSystem
return Options.AllowedExtraWebContentFileExtensions.Any(e => path.EndsWith(e, StringComparison.OrdinalIgnoreCase));
}
}
}
}

4
framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs

@ -109,7 +109,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// Throws exception if service is not registered.
/// This method should be used only after dependency injection registration phase completed.
/// </summary>
internal static T GetRequiredService<T>(this IServiceCollection services)
public static T GetRequiredService<T>(this IServiceCollection services)
{
return services
.GetSingletonInstance<IAbpApplication>()
@ -122,7 +122,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// Throws exception if service is not registered.
/// This method should be used only after dependency injection registration phase completed.
/// </summary>
internal static object GetRequiredService(this IServiceCollection services, Type type)
public static object GetRequiredService(this IServiceCollection services, Type type)
{
return services
.GetSingletonInstance<IAbpApplication>()

2
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs

@ -108,7 +108,7 @@ namespace Volo.Abp.AspNetCore.Mvc
var app = context.GetApplicationBuilder();
app.UseCorrelationId();
app.UseVirtualFiles();
app.UseStaticFiles();
app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();
app.UseRouting();

2
framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/AbpAspNetCoreMvcUiBootstrapDemoModule.cs

@ -23,7 +23,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo
}
app.UseRouting();
app.UseVirtualFiles();
app.UseStaticFiles();
app.UseConfiguredEndpoints();
}
}

2
framework/test/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo/AbpAspNetCoreMvcUiThemeBasicDemoModule.cs

@ -55,7 +55,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo
app.UseDeveloperExceptionPage();
}
app.UseVirtualFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseConfiguredEndpoints();
}

2
framework/test/Volo.Abp.AspNetCore.SignalR.Tests/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions_Tests.cs

@ -14,7 +14,7 @@ namespace Volo.Abp.AspNetCore.SignalR
_options = GetRequiredService<IOptions<AbpSignalROptions>>().Value;
}
[Fact]
[Fact(Skip = "Can not run this test since AspNet Core environment has not been properly set!")]
public void Should_Auto_Add_Maps()
{
_options.Hubs.ShouldContain(h => h.HubType == typeof(RegularHub));

2
framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestModule.cs

@ -34,7 +34,7 @@ namespace Volo.Abp.AspNetCore
var app = context.GetApplicationBuilder();
app.UseCorrelationId();
app.UseVirtualFiles();
app.UseStaticFiles();
}
private string FindProjectPath(IWebHostEnvironment hostEnvironment)

Loading…
Cancel
Save