Skip to content

The dynamic type created is thrown FileLoadException when using the GetCustomAttributes method #67802

@kai-w-wang

Description

@kai-w-wang

Which version of .NET you have tested on: Version: 6.0.3; Commit: c24d9a9
Which OS and architecture were you using: Windows 11 x64
Expected result: System.Reflection.CustomAttribute.GetCustomAttributes works with dynamic Type (AssemblyBuilder/ModuleBuilder).
Actual result: FileLoadException

The issue seems related to System.Reflection.Emit.AssemblyBuilder.Location

       // System.Reflection.Emit.AssemblyBuilder
	public override string Location
	{
		get
		{
			throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
		}
	}

The Location property was being used by System.Reflection.Assembly.LoadFromResolveHandler which triggers FileLoadException

// System.Reflection.Assembly
private static Assembly LoadFromResolveHandler(object sender, ResolveEventArgs args)
{
	//...
	string fullPath = Path.GetFullPath(requestingAssembly.Location);
	//...
}

Call Stack

System.IO.FileLoadException
  HResult=0x80131515
  Message=Could not load file or assembly 'ODataExtensionec551e7e1c3540a1b486e4be222060dc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Operation is not supported. (0x80131515)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
   at System.Reflection.RuntimeMethodInfo.GetCustomAttributes(Boolean inherit)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.CreateActionModel(TypeInfo typeInfo, MethodInfo methodInfo)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.OnProvidersExecuting(ApplicationModelProviderContext context)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.CreateApplicationModel(IEnumerable`1 controllerTypes)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0()
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()
   at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence)
   at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints)
   at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
   at Program.Main(String[] args) in C:\src\herbalife\hlfdmp\api\DataPlatform.Api\Program.cs:line 71

  This exception was originally thrown at this call stack:
    System.Reflection.Emit.InternalAssemblyBuilder.Location.get()
    System.Reflection.Assembly.LoadFromResolveHandler(object, System.ResolveEventArgs)
    System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(System.ResolveEventHandler, System.Reflection.RuntimeAssembly, string)
    System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(System.Reflection.RuntimeAssembly, string)

Inner Exception 1:
NotSupportedException: The invoked member is not supported in a dynamic assembly.

Related issue: #26912

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions