Dynamic assembly checks to prevent NotSupprotedExceptions from System.Reflection.Emit

Fixes #5487
This commit is contained in:
Marcus Schweda 2016-11-03 15:25:04 +01:00 committed by Pranav K
parent 9caa688a30
commit 24d5dfb552
4 changed files with 48 additions and 1 deletions

View File

@ -47,6 +47,13 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
/// <inheritdoc />
public IEnumerable<string> 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<string>();
}
var dependencyContext = DependencyContext.Load(Assembly);
if (dependencyContext != null)
{

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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<AssemblyPart, Type> _containerLookup;