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 224017c762
commit 7f2a64e32b
No known key found for this signature in database
GPG Key ID: 1963DA6D96C3057A
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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -59,15 +61,15 @@ namespace Microsoft.Extensions.DependencyInjection
private static void AddDefaultFrameworkParts(ApplicationPartManager partManager) private static void AddDefaultFrameworkParts(ApplicationPartManager partManager)
{ {
var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly; 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; 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; 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. // Ensures that the entry assembly part is marked correctly.
var assemblyPartMetadata = applicationPartManager var assemblyPartMetadata = applicationPartManager
.ApplicationParts .ApplicationParts
.Where(part => part.GetType() == typeof(AssemblyPart)) .OfType<AssemblyPart>()
.Select(part => part.Name) .Select(part => part.Name)
.ToArray(); .ToArray();