Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 1d085cd

Browse files
committed
Modify Razor components to use RazorViewEngineOptions.FileSystem
Fixes #1302
1 parent 6fe6639 commit 1d085cd

File tree

12 files changed

+151
-94
lines changed

12 files changed

+151
-94
lines changed

src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
using Microsoft.AspNet.Razor.Parser;
1212
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
1313

14-
#if ASPNET50 || ASPNETCORE50
15-
using Microsoft.Framework.Runtime;
16-
#endif
17-
1814
namespace Microsoft.AspNet.Mvc.Razor
1915
{
2016
public class MvcRazorHost : RazorEngineHost, IMvcRazorHost
@@ -41,17 +37,7 @@ public class MvcRazorHost : RazorEngineHost, IMvcRazorHost
4137
// This field holds the type name without the generic decoration (MyBaseType)
4238
private readonly string _baseType;
4339

44-
#if ASPNET50 || ASPNETCORE50
45-
/// <summary>
46-
/// Initializes a new instance of <see cref="MvcRazorHost"/> with the specified
47-
/// <param name="appEnvironment"/>.
48-
/// </summary>
49-
/// <param name="appEnvironment">Contains information about the executing application.</param>
50-
public MvcRazorHost(IApplicationEnvironment appEnvironment)
51-
: this(new PhysicalFileSystem(appEnvironment.ApplicationBasePath))
52-
{
53-
}
54-
#elif NET45
40+
#if NET45
5541
/// <summary>
5642
/// Initializes a new instance of <see cref="MvcRazorHost"/> with the specified
5743
/// <param name="root"/>.
@@ -62,12 +48,11 @@ public MvcRazorHost(string root) :
6248
{
6349
}
6450
#endif
65-
6651
/// <summary>
6752
/// Initializes a new instance of <see cref="MvcRazorHost"/> using the specified <paramref name="fileSystem"/>.
6853
/// </summary>
6954
/// <param name="fileSystem">A <see cref="IFileSystem"/> rooted at the application base path.</param>
70-
protected internal MvcRazorHost([NotNull] IFileSystem fileSystem)
55+
public MvcRazorHost([NotNull] IFileSystem fileSystem)
7156
: base(new CSharpRazorCodeLanguage())
7257
{
7358
_fileSystem = fileSystem;

src/Microsoft.AspNet.Mvc.Razor/Compilation/ExpiringFileInfoCache.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Concurrent;
66
using Microsoft.AspNet.FileSystems;
77
using Microsoft.Framework.OptionsModel;
8-
using Microsoft.Framework.Runtime;
98

109
namespace Microsoft.AspNet.Mvc.Razor
1110
{
@@ -17,15 +16,13 @@ public class ExpiringFileInfoCache : IFileInfoCache
1716
private readonly ConcurrentDictionary<string, ExpiringFileInfo> _fileInfoCache =
1817
new ConcurrentDictionary<string, ExpiringFileInfo>(StringComparer.Ordinal);
1918

20-
private readonly PhysicalFileSystem _fileSystem;
19+
private readonly IFileSystem _fileSystem;
2120
private readonly TimeSpan _offset;
2221

23-
protected virtual IFileSystem FileSystem
22+
public ExpiringFileInfoCache(IOptionsAccessor<RazorViewEngineOptions> optionsAccessor)
2423
{
25-
get
26-
{
27-
return _fileSystem;
28-
}
24+
_fileSystem = optionsAccessor.Options.FileSystem;
25+
_offset = optionsAccessor.Options.ExpirationBeforeCheckingFilesOnDisk;
2926
}
3027

3128
protected virtual DateTime UtcNow
@@ -36,14 +33,6 @@ protected virtual DateTime UtcNow
3633
}
3734
}
3835

39-
public ExpiringFileInfoCache(IApplicationEnvironment env,
40-
IOptionsAccessor<RazorViewEngineOptions> optionsAccessor)
41-
{
42-
// TODO: Inject the IFileSystem but only when we get it from the host
43-
_fileSystem = new PhysicalFileSystem(env.ApplicationBasePath);
44-
_offset = optionsAccessor.Options.ExpirationBeforeCheckingFilesOnDisk;
45-
}
46-
4736
/// <inheritdoc />
4837
public IFileInfo GetFileInfo([NotNull] string virtualPath)
4938
{
@@ -59,7 +48,7 @@ public IFileInfo GetFileInfo([NotNull] string virtualPath)
5948
}
6049
else
6150
{
62-
FileSystem.TryGetFileInfo(virtualPath, out fileInfo);
51+
_fileSystem.TryGetFileInfo(virtualPath, out fileInfo);
6352

6453
expiringFileInfo = new ExpiringFileInfo()
6554
{

src/Microsoft.AspNet.Mvc.Razor/OptionDescriptors/RazorViewEngineOptions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.AspNet.FileSystems;
67
using Microsoft.AspNet.Mvc.Razor.OptionDescriptors;
78

89
namespace Microsoft.AspNet.Mvc.Razor
@@ -13,6 +14,7 @@ namespace Microsoft.AspNet.Mvc.Razor
1314
public class RazorViewEngineOptions
1415
{
1516
private TimeSpan _expirationBeforeCheckingFilesOnDisk = TimeSpan.FromSeconds(2);
17+
private IFileSystem _fileSystem;
1618

1719
/// <summary>
1820
/// Controls the <see cref="ExpiringFileInfoCache" /> caching behavior.
@@ -47,5 +49,28 @@ public TimeSpan ExpirationBeforeCheckingFilesOnDisk
4749
/// </summary>
4850
public IList<ViewLocationExpanderDescriptor> ViewLocationExpanders { get; }
4951
= new List<ViewLocationExpanderDescriptor>();
52+
53+
/// <summary>
54+
/// Gets or sets the <see cref="IFileSystem" /> used by <see cref="RazorViewEngine"/> to locate Razor files on
55+
/// disk.
56+
/// </summary>
57+
/// <remarks>
58+
/// At startup, this is initialized to an instance of <see cref="PhysicalFileSystem"/> that is rooted at the
59+
/// application root.
60+
/// </remarks>
61+
public IFileSystem FileSystem
62+
{
63+
get { return _fileSystem; }
64+
65+
set
66+
{
67+
if (value == null)
68+
{
69+
throw new ArgumentNullException(nameof(value));
70+
}
71+
72+
_fileSystem = value;
73+
}
74+
}
5075
}
5176
}

src/Microsoft.AspNet.Mvc.Razor/ViewStartProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Microsoft.AspNet.FileSystems;
8-
using Microsoft.Framework.Runtime;
8+
using Microsoft.Framework.OptionsModel;
99

1010
namespace Microsoft.AspNet.Mvc.Razor
1111
{
@@ -15,10 +15,10 @@ public class ViewStartProvider : IViewStartProvider
1515
private readonly IFileSystem _fileSystem;
1616
private readonly IRazorPageFactory _pageFactory;
1717

18-
public ViewStartProvider(IApplicationEnvironment appEnv,
19-
IRazorPageFactory pageFactory)
18+
public ViewStartProvider(IRazorPageFactory pageFactory,
19+
IOptionsAccessor<RazorViewEngineOptions> optionsAccessor)
2020
{
21-
_fileSystem = new PhysicalFileSystem(appEnv.ApplicationBasePath);
21+
_fileSystem = optionsAccessor.Options.FileSystem;
2222
_pageFactory = pageFactory;
2323
}
2424

src/Microsoft.AspNet.Mvc.Razor/VirtualPathRazorPageFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNet.Mvc.Core;
65
using Microsoft.Framework.DependencyInjection;
76

87
namespace Microsoft.AspNet.Mvc.Razor

src/Microsoft.AspNet.Mvc/MvcServices.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration
3232
var describe = new ServiceDescriber(configuration);
3333

3434
yield return describe.Transient<IOptionsAction<MvcOptions>, MvcOptionsSetup>();
35+
yield return describe.Transient<IOptionsAction<RazorViewEngineOptions>, RazorViewEngineOptionsSetup>();
3536

3637
// Only want one ITagHelperActivator so it can cache Type activation information. Types won't conflict.
3738
yield return describe.Singleton<ITagHelperActivator, DefaultTagHelperActivator>();
@@ -52,7 +53,11 @@ public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration
5253
yield return describe.Transient<IActionDiscoveryConventions, DefaultActionDiscoveryConventions>();
5354

5455
// The host is designed to be discarded after consumption and is very inexpensive to initialize.
55-
yield return describe.Transient<IMvcRazorHost, MvcRazorHost>();
56+
yield return describe.Transient<IMvcRazorHost>(serviceProvider =>
57+
{
58+
var optionsAccessor = serviceProvider.GetService<IOptionsAccessor<RazorViewEngineOptions>>();
59+
return new MvcRazorHost(optionsAccessor.Options.FileSystem);
60+
});
5661

5762
yield return describe.Singleton<ICompilationService, RoslynCompilationService>();
5863
yield return describe.Singleton<IRazorCompilationService, RazorCompilationService>();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.FileSystems;
5+
using Microsoft.AspNet.Mvc.Razor;
6+
using Microsoft.Framework.OptionsModel;
7+
using Microsoft.Framework.Runtime;
8+
9+
namespace Microsoft.AspNet.Mvc
10+
{
11+
/// <summary>
12+
/// Sets up default options for <see cref="RazorViewEngineOptions"/>.
13+
/// </summary>
14+
public class RazorViewEngineOptionsSetup : OptionsAction<RazorViewEngineOptions>
15+
{
16+
public RazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment)
17+
: base(options => ConfigureRazor(options, applicationEnvironment))
18+
{
19+
Order = -1;
20+
}
21+
22+
private static void ConfigureRazor(RazorViewEngineOptions razorOptions,
23+
IApplicationEnvironment applicationEnvironment)
24+
{
25+
razorOptions.FileSystem = new PhysicalFileSystem(applicationEnvironment.ApplicationBasePath);
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)