A customer reported a problem in which one of my programs would crash when they tried to run it. They provided a dump file generated during the crash. The dump file showed that the problem was a System.ExecutionEngineException being thrown when I was extracting a DLL embedded as a resource in the program.
The code in question is:
public FrontEnd()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args2) =>
{
var resourceName = new AssemblyName(args2.Name).Name + ".dll";
var resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName, StringComparison.CurrentCulture));
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
var assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
this.InitializeComponent();
}
I am using an approach described on this website, and have had no problems with it until now.
The exception is thrown on the GetManifestResourceStream() line.
For information this code has worked on many PCs around the world for several months, but not for this customer.
Running the program in administrator mode made no difference.
Both the program and the DLL are written in C# (4.0).
I have tried disabling concurrent garbage collection as suggested in this stackoverflow post.
One of my colleagues asked the customer to try another program that uses the same technique, and it had the same problem.
I have worked around this for now by commenting out this code. But I would like to know: why is this happening? How do I fix it properly? Is there a better way to embed DLLs? [FYI, I started doing this after several customers had issues with copying the program and forgetting to also copy the then-separate DLLs].