From 24d5dfb5527d46da097b56b7cfc3b28ec8f8ad3e Mon Sep 17 00:00:00 2001 From: Marcus Schweda Date: Thu, 3 Nov 2016 15:25:04 +0100 Subject: [PATCH] Dynamic assembly checks to prevent NotSupprotedExceptions from System.Reflection.Emit Fixes #5487 --- .../ApplicationParts/AssemblyPart.cs | 7 +++++++ .../Compilation/ViewsFeatureProvider.cs | 2 +- .../ApplicationParts/AssemblyPartTest.cs | 19 +++++++++++++++++ .../Compilation/ViewsFeatureProviderTest.cs | 21 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/AssemblyPart.cs b/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/AssemblyPart.cs index a091013710..f18da3a406 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/AssemblyPart.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/AssemblyPart.cs @@ -47,6 +47,13 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts /// public IEnumerable GetReferencePaths() { + if (Assembly.IsDynamic) + { + // Skip loading process for dynamic assemblies. This prevents DependencyContextLoader from reading the + // .deps.json file from either manifest resources or the assembly location, which will fail. + return Enumerable.Empty(); + } + var dependencyContext = DependencyContext.Load(Assembly); if (dependencyContext != null) { diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs index b55052589e..407b7c8e12 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/ViewsFeatureProvider.cs @@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation protected virtual Type GetViewInfoContainerType(AssemblyPart assemblyPart) { #if NETSTANDARD1_6 - if (assemblyPart.Assembly.Location != null) + if (!assemblyPart.Assembly.IsDynamic && assemblyPart.Assembly.Location != null) { var precompiledAssemblyFileName = assemblyPart.Assembly.GetName().Name + PrecompiledViewsAssemblySuffix diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/AssemblyPartTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/AssemblyPartTest.cs index b25e2b3ffc..b922a9eff0 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/AssemblyPartTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/AssemblyPartTest.cs @@ -1,9 +1,11 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using Xunit; namespace Microsoft.AspNetCore.Mvc.ApplicationParts @@ -81,5 +83,22 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts var actual = Assert.Single(references); Assert.Equal(assembly.Location, actual); } + + [Fact] + public void GetReferencePaths_ReturnsEmptySequenceForDynamicAssembly() + { + // Arrange + var name = new AssemblyName($"DynamicAssembly-{Guid.NewGuid()}"); + var assembly = AssemblyBuilder.DefineDynamicAssembly(name, + AssemblyBuilderAccess.RunAndCollect); + + var part = new AssemblyPart(assembly); + + // Act + var references = part.GetReferencePaths().ToList(); + + // Assert + Assert.Empty(references); + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Compilation/ViewsFeatureProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Compilation/ViewsFeatureProviderTest.cs index 081ff63254..5a41914aac 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Compilation/ViewsFeatureProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Compilation/ViewsFeatureProviderTest.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Xunit; @@ -69,6 +70,26 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation }); } + [Fact] + public void PopulateFeature_ReturnsEmptySequenceIfNoDynamicAssemblyPartHasViewAssembly() + { + // Arrange + var name = new AssemblyName($"DynamicAssembly-{Guid.NewGuid()}"); + var assembly = AssemblyBuilder.DefineDynamicAssembly(name, + AssemblyBuilderAccess.RunAndCollect); + + var applicationPartManager = new ApplicationPartManager(); + applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly)); + applicationPartManager.FeatureProviders.Add(new ViewsFeatureProvider()); + var feature = new ViewsFeature(); + + // Act + applicationPartManager.PopulateFeature(feature); + + // Assert + Assert.Empty(feature.Views); + } + private class TestableViewsFeatureProvider : ViewsFeatureProvider { private readonly Dictionary _containerLookup;