I am not saying this should not be happening for you, I did not try to use demand loading via the registry so I am not sure if this would change the behavior of how my loading is working.
This isn't the first time I have used the libraries that started giving me problems now. The same method of loading up the dependencies occurs in at least four other pieces of software. I have never run into trouble until the autoloader.
Try throw a message box up with Environment.CurrentDirectory at the init of your add in. If it says like it does for me when I Netload that it is in fact the directory of your assembly, then your code is running just like mine is when I Netload, and if I would have tried this everything would be fine. If it says My Documents then you are doing something right that I have been unable to without setting the CurrentDirectory manually.
I added an event handler to the AssembyResolve event like you showed but it was never called and I could never find out why.
I used the following:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
I added code to show a message box with the calling assembly name and what it was trying to resolve, but it never did show up.
All I know is at the end of the day I can take the code that I first put up on here in my first message, add the lines:
string filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
string completeFilePath = System.IO.Path.GetDirectoryName(filePath);
Environment.CurrentDirectory = completeFilePath;
And it will work. So if my code throws errors trying to resolve assemblies in my documents, and then I add these three lines and everything works, *something* must be related to Environment.CurrentDirectory. I am just describing why I think this is.
- This only occurs in my autoloader solution, all other software using this does not have this problem.
- When I use netload the currentdirectory is the correct directory of my add in assembly
- When I use autoload the currentdirectory is my documents
- The only change between netload and autoload software is the above three lines
- After these three lines are added then the autoload solution works properly
Those above 5 statements are the only ones that I will stick to, those are the 5 facts I know for sure can be screen captured and the source files diff'd to verify there are in fact no embelishments.
The rest of it is my interpretation on what may be happneing in the background based on what I have read in other forums, and the above 5 things.
I am interested in getting assembly resolve working since this seems like a more robust way of handling these things, and adds the extra benefit of load from other directories when necessary.
I am also open to anyone who can take the above 5 things I have observed, and add the rest of the story to it if I am wrong.
I found post that got me looking into the LoadFrom CurrentDirectory operation:
http://stackoverflow.com/questions/3187088/could-not-load-file-or-assembly
Quote:
The method LoadFrom will use Environment.CurrentDirectory to build the full path to the assembly that will be loaded. The current directory is not the same as the application base path.
You should provide the full path to LoadFrom method, you can build it using AppDomain.CurrentDomain.BaseDirectory if the file is located in the same folder then the executable.
The loadfrom exception states it can't load my file from My Documents -> The Environment.CurrentDirectory is My Documents. Change Environment.CurrentDirectory to the assembly path and it loads. Remember though I am not trying to say your code should not work, this is the ONE time I have had to do this, there is only one way of using my source that I have had to modify to get working. In the vast majority of cases when you look at this var it will be the correct path.
To avoid these problems though and to avoid others I see you could run into you should:
- Not use LoadFrom -> Use Load instead
- Build the full path in your code and pass this into any loading functions
- Get the AssemblyResolve event handler working so you can catch these sorts of things and deal with them in one place.