From 7f2a64e32beb9d08cfcb5e2158605ce84a886c95 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 29 Jun 2018 09:41:41 -0700 Subject: [PATCH] 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 --- .../MvcServiceCollectionExtensions.cs | 23 +++++++++++++++---- .../Controllers/HomeController.cs | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs index b7baee8634..037bcdd2d3 100644 --- a/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs @@ -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 @@ public static IMvcBuilder AddMvc(this IServiceCollection services) private static void AddDefaultFrameworkParts(ApplicationPartManager partManager) { var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly; - if(!partManager.ApplicationParts.OfType().Any(p => p.Assembly == mvcTagHelpersAssembly)) + if (!partManager.ApplicationParts.OfType().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().Any(p => p.Assembly == mvcRazorAssembly)) + if (!partManager.ApplicationParts.OfType().Any(p => p.Assembly == mvcRazorAssembly)) { - partManager.ApplicationParts.Add(new AssemblyPart(mvcRazorAssembly)); + partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcRazorAssembly)); } } @@ -94,5 +96,16 @@ public static IMvcBuilder AddMvc(this IServiceCollection services, Action ICompilationReferencesProvider.GetReferencePaths() => Enumerable.Empty(); + } } } diff --git a/test/WebSites/BasicWebSite/Controllers/HomeController.cs b/test/WebSites/BasicWebSite/Controllers/HomeController.cs index 454aca34b8..5d5fb11525 100644 --- a/test/WebSites/BasicWebSite/Controllers/HomeController.cs +++ b/test/WebSites/BasicWebSite/Controllers/HomeController.cs @@ -129,7 +129,7 @@ public IActionResult GetAssemblyPartData([FromServices] ApplicationPartManager a // Ensures that the entry assembly part is marked correctly. var assemblyPartMetadata = applicationPartManager .ApplicationParts - .Where(part => part.GetType() == typeof(AssemblyPart)) + .OfType() .Select(part => part.Name) .ToArray();