29

We have custom DLL's that are not included in our initial setup file. They are loaded at runtime. This process worked fine while using .NET 2.0, but we are getting the "The invoked member is not supported in a dynamic assembly" error message now that we are using .NET 4.0.

try
{
    assem = Assembly.LoadFrom(fi.FullName); //fi is FileSystemInfo
}
catch (FileLoadException) {}
catch (BadImageFormatException) {}
catch (System.Security.SecurityException) {}
catch (ArgumentException) {}
catch (PathTooLongException) {}
Joel
  • 7,401
  • 4
  • 52
  • 58
Trevorm
  • 505
  • 1
  • 5
  • 9

5 Answers5

34

This error is occurring because Assembly.Load cannot be called upon dynamic assemblies. You must filter out the dynamic assemblies before using them.

var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(p => !p.IsDynamic);

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • 2
    The problem is in loading assemblies ... how can you do this on assemblies you haven't yet loaded? Assembly.LoadFile(assemblyPath) ... all you have is a file path. – War Aug 28 '19 at 08:52
  • 2
    I was getting this error calling `assembly.Location`. Filtering out `IsDynamic` was the fix for me – Graham Apr 05 '20 at 13:04
29

For me this issue was not embedding the license for a Aspose dll: http://www.aspose.com/community/forums/thread/423874/initializing-the-license-file.aspx

Their code injects dynamic assemblies when a license isn't detected, causing their DLLs to fail, as well as a bunch of other code that isn't compatible with dynamic assemblies.

Not sure if this is a common licensing/activation method for ensuring registered use with 3rd party dlls, so I'll post it here for google if it is.

user326608
  • 2,210
  • 1
  • 26
  • 33
4

This in the app.config file allows for "plug-in" dll's from remote sources.

<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>

http://msdn.microsoft.com/en-us/library/dd409252.aspx

Trevorm
  • 505
  • 1
  • 5
  • 9
0

I ran into the same error. There is a method in our codebase that walks the assemblies loaded in the current AppDomain, and looks for a given resource by name.

        Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
        
        foreach (Assembly tempAssembly in allAssemblies)
        {
            Stream resourceStream = tempAssembly.GetManifestResourceStream(resourceName);
            // ...
            
        }
        

If it so happens that we stumble upon a dynamic assembly, the call to GetManifestResourceStream fails with the "The invoked member is not supported in a dynamic assembly" error.

Sebacote
  • 690
  • 6
  • 17
0

I spend many hours to figure out this issue.

We were loading DLL of another Class Library project which in turn creates the Instance on Fly. So Below worked for me.

Solution:

Add Reference of the DLL to main project.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196