Dynamic assembly checks to prevent NotSupprotedExceptions from System.Reflection.Emit
Fixes #5487
This commit is contained in:
parent
9caa688a30
commit
24d5dfb552
|
|
@ -47,6 +47,13 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IEnumerable<string> GetReferencePaths()
|
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);
|
var dependencyContext = DependencyContext.Load(Assembly);
|
||||||
if (dependencyContext != null)
|
if (dependencyContext != null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
|
||||||
protected virtual Type GetViewInfoContainerType(AssemblyPart assemblyPart)
|
protected virtual Type GetViewInfoContainerType(AssemblyPart assemblyPart)
|
||||||
{
|
{
|
||||||
#if NETSTANDARD1_6
|
#if NETSTANDARD1_6
|
||||||
if (assemblyPart.Assembly.Location != null)
|
if (!assemblyPart.Assembly.IsDynamic && assemblyPart.Assembly.Location != null)
|
||||||
{
|
{
|
||||||
var precompiledAssemblyFileName = assemblyPart.Assembly.GetName().Name
|
var precompiledAssemblyFileName = assemblyPart.Assembly.GetName().Name
|
||||||
+ PrecompiledViewsAssemblySuffix
|
+ PrecompiledViewsAssemblySuffix
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
|
|
@ -81,5 +83,22 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
var actual = Assert.Single(references);
|
var actual = Assert.Single(references);
|
||||||
Assert.Equal(assembly.Location, actual);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||||
using Xunit;
|
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 class TestableViewsFeatureProvider : ViewsFeatureProvider
|
||||||
{
|
{
|
||||||
private readonly Dictionary<AssemblyPart, Type> _containerLookup;
|
private readonly Dictionary<AssemblyPart, Type> _containerLookup;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue