Razor runtime compilation produces errors if running on a shared runtime that's rolled forward

Do not provide compilation references from runtime MVC assemblies. This avoids cases where the app is compiled
against an older MVC but running against a newer one (e.g. shared fx roll forward) resulting in compiling against multiple
versions of MVC assemblies

Fixes #7969
This commit is contained in:
Pranav K 2018-06-29 09:41:41 -07:00
parent 78fc49ba0c
commit c4d5ef94a9
2 changed files with 19 additions and 6 deletions

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
@ -59,15 +61,15 @@ namespace Microsoft.Extensions.DependencyInjection
private static void AddDefaultFrameworkParts(ApplicationPartManager partManager)
{
var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
if(!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcTagHelpersAssembly))
if (!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcTagHelpersAssembly))
{
partManager.ApplicationParts.Add(new AssemblyPart(mvcTagHelpersAssembly));
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcTagHelpersAssembly));
}
var mvcRazorAssembly = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
if(!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcRazorAssembly))
if (!partManager.ApplicationParts.OfType<AssemblyPart>().Any(p => p.Assembly == mvcRazorAssembly))
{
partManager.ApplicationParts.Add(new AssemblyPart(mvcRazorAssembly));
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcRazorAssembly));
}
}
@ -94,5 +96,16 @@ namespace Microsoft.Extensions.DependencyInjection
return builder;
}
[DebuggerDisplay("{Name}")]
private class FrameworkAssemblyPart : AssemblyPart, ICompilationReferencesProvider
{
public FrameworkAssemblyPart(Assembly assembly)
: base(assembly)
{
}
IEnumerable<string> ICompilationReferencesProvider.GetReferencePaths() => Enumerable.Empty<string>();
}
}
}

View File

@ -129,7 +129,7 @@ namespace BasicWebSite.Controllers
// Ensures that the entry assembly part is marked correctly.
var assemblyPartMetadata = applicationPartManager
.ApplicationParts
.Where(part => part.GetType() == typeof(AssemblyPart))
.OfType<AssemblyPart>()
.Select(part => part.Name)
.ToArray();