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

Commit

Permalink
Razor runtime compilation produces errors if running on a shared runt…
Browse files Browse the repository at this point in the history
…ime 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
  • Loading branch information
pranavkm committed Jun 29, 2018
1 parent 224017c commit 7f2a64e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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));
}
}

Expand All @@ -94,5 +96,16 @@ public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOpt

return builder;
}

[DebuggerDisplay("{Name}")]
private class FrameworkAssemblyPart : AssemblyPart, ICompilationReferencesProvider
{
public FrameworkAssemblyPart(Assembly assembly)
: base(assembly)
{
}

IEnumerable<string> ICompilationReferencesProvider.GetReferencePaths() => Enumerable.Empty<string>();
}
}
}
2 changes: 1 addition & 1 deletion test/WebSites/BasicWebSite/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssemblyPart>()
.Select(part => part.Name)
.ToArray();

Expand Down

0 comments on commit 7f2a64e

Please sign in to comment.