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

Commit

Permalink
Register DependencyContextRazorViewEngineOptionsSetup after RazorView…
Browse files Browse the repository at this point in the history
…EngineOptionsSetup

Fixes #4902
  • Loading branch information
pranavkm committed Jul 15, 2016
1 parent 310ab25 commit 82decdb
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,20 @@ public static IMvcCoreBuilder InitializeTagHelper<TTagHelper>(
// Internal for testing.
internal static void AddRazorViewEngineServices(IServiceCollection services)
{
services.TryAddEnumerable(
ServiceDescriptor.Transient<
IConfigureOptions<RazorViewEngineOptions>,
DependencyContextRazorViewEngineOptionsSetup>());

// This caches compilation related details that are valid across the lifetime of the application.
services.TryAddSingleton<ICompilationService, DefaultRoslynCompilationService>();

services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcViewOptions>, MvcRazorMvcViewOptionsSetup>());

// DependencyContextRazorViewEngineOptionsSetup needs to run after RazorViewEngineOptionsSetup.
// The ordering of the following two lines is important to ensure this behavior.
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<RazorViewEngineOptions>, RazorViewEngineOptionsSetup>());
services.TryAddEnumerable(
ServiceDescriptor.Transient<
IConfigureOptions<RazorViewEngineOptions>,
DependencyContextRazorViewEngineOptionsSetup>());

services.TryAddSingleton<
IRazorViewEngineFileProviderAccessor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,54 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Options;
using DependencyContextOptions = Microsoft.Extensions.DependencyModel.CompilationOptions;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
/// <summary>
/// Sets up compilation and parse option default options for <see cref="RazorViewEngineOptions"/> using
/// <see cref="DependencyContext"/>
/// </summary>
public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptions>
public class DependencyContextRazorViewEngineOptionsSetup : IConfigureOptions<RazorViewEngineOptions>
{
private readonly IHostingEnvironment _hostingEnvironment;

/// <summary>
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
/// </summary>
public DependencyContextRazorViewEngineOptionsSetup(IHostingEnvironment hostingEnvironment)
: base(options => ConfigureRazor(options, hostingEnvironment))
{
_hostingEnvironment = hostingEnvironment;
}

private static void ConfigureRazor(RazorViewEngineOptions options, IHostingEnvironment hostingEnvironment)
/// <inheritdoc />
public void Configure(RazorViewEngineOptions options)
{
var applicationAssembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
var dependencyContext = DependencyContext.Load(applicationAssembly);
var compilationOptions = dependencyContext?.CompilationOptions ?? Extensions.DependencyModel.CompilationOptions.Default;
var compilationOptions = GetCompilationOptions();

SetParseOptions(options, compilationOptions);
SetCompilationOptions(options, compilationOptions);
}

private static void SetCompilationOptions(RazorViewEngineOptions options, Extensions.DependencyModel.CompilationOptions compilationOptions)
// Internal for unit testing.
protected internal virtual DependencyContextOptions GetCompilationOptions()
{
if (!string.IsNullOrEmpty(_hostingEnvironment.ApplicationName))
{
var applicationAssembly = Assembly.Load(new AssemblyName(_hostingEnvironment.ApplicationName));
var dependencyContext = DependencyContext.Load(applicationAssembly);
if (dependencyContext?.CompilationOptions != null)
{
return dependencyContext.CompilationOptions;
}
}

return DependencyContextOptions.Default;
}

private static void SetCompilationOptions(
RazorViewEngineOptions options,
DependencyContextOptions compilationOptions)
{
var roslynOptions = options.CompilationOptions;

Expand All @@ -57,17 +77,17 @@ private static void SetCompilationOptions(RazorViewEngineOptions options, Extens

if (compilationOptions.Optimize.HasValue)
{
var optimizationLevel = compilationOptions.Optimize.Value
? OptimizationLevel.Debug
: OptimizationLevel.Release;
var optimizationLevel = compilationOptions.Optimize.Value ?
OptimizationLevel.Release :
OptimizationLevel.Debug;
roslynOptions = roslynOptions.WithOptimizationLevel(optimizationLevel);
}

if (compilationOptions.WarningsAsErrors.HasValue)
{
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value
? ReportDiagnostic.Error
: ReportDiagnostic.Default;
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value ?
ReportDiagnostic.Error :
ReportDiagnostic.Default;
roslynOptions = roslynOptions.WithGeneralDiagnosticOption(reportDiagnostic);
}

Expand All @@ -76,7 +96,7 @@ private static void SetCompilationOptions(RazorViewEngineOptions options, Extens

private static void SetParseOptions(
RazorViewEngineOptions options,
Extensions.DependencyModel.CompilationOptions compilationOptions)
DependencyContextOptions compilationOptions)
{
var parseOptions = options.ParseOptions;
parseOptions = parseOptions.WithPreprocessorSymbols(
Expand Down
Loading

0 comments on commit 82decdb

Please sign in to comment.