mercredi 2 janvier 2019

Static classes are null from within dynamically loaded assembly

I'm loading an external assembly, in which I've set a breakpoint and static classes like WebUtility are null at runtime when the breakpoint is hit.

Here's how I load the external assembly:

//We have to write the plugin assembly's PDB file to web server so debugging (or remote debugging works)
var binPath = HttpRuntime.BinDirectory;
var pdbFileName = $@"{binPath}{Path.GetFileNameWithoutExtension(AssemblyFilename)}.pdb";

try
{
    File.WriteAllBytes(pdbFileName, ByteArrayForPDBFile);
}
catch (Exception ex)
{
    throw new Exception($"An error occured copying plugin assembly symbol file to executing directory: {ex.Message}");
}

//I've also tried AppDomain.CurrentDomain.Load() here as well...
var assembly = Assembly.Load(ByteArrayForAssembly, ByteArrayForPDBFile);
IPlugin casted;

var instance = assembly.CreateInstance(TypeNameToInit);
casted = (IPlugin)instance;

casted.Execute();

This code is executing in the context of an ASP MVC web application.

I've also noted that AppDomain is null when the breakpoint inside the external assembly is hit, which is a bit odd I would think. Do I need to somehow inject the AppDomain of the application loaded the external assembly ?

Thanks.





Aucun commentaire:

Enregistrer un commentaire