System.IO.FileNotFoundException occuring in mscorlib.dll?
Background: I am working on a very large Suite that contains about 12 different sub-applications. My goal is to take one of these sub-applications and create its own executable with all the DLLs embedded into the EXE file. So far, I have been able to embed most of the DLLs through the following steps
1) Add the DLLs to the project in a local Folder and set them to "Embedded Resources".
2) Add those DLLs to the References tab and set the Copy Local property to False
3) Add those DLLs to the Resources.resx file
I have also created a new Program.cs file and set the entry point to the main function located in the desired sub application assembly. My problem is that the DLL file that is used to run the entire suite cannot be embedded into the EXE of the subapplication because I keep getting a System.IO.FileNotFoundException in the mscorlib.dll with the stack frame set at System.AppDomain.ExecuteAssembly. I however can completely avoid this exception is I set the "Suite.dll" Copy Local property to true.
Here is my assembly resolver handler, but it never gets hit since the exception is occuring in managed code before the Main function is even executed.
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name+".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
Is this even preventable? How do I resolve this dependency so that I can embed my Suite DLL file into my sub-application EXE? Please comment if more information is required and I will edit the question. Thanks!
Solution 1:
Reduce your main function to setting up AssemblyResolve and then calling another function in another class that does whatever you have main doing now.
In order to use this trick, neither main, nor the containing class may reference any of the packed assemblies, else they can't be resolved when main is jitted.
Solution 2:
I had the same problem, the reason was that I accidentially deleted Newtonsoft.Json.dll
from my project folder.