C# assembly load from file
If I follow you correctly and I may not , you're loading add-ins by folder now, not by DLL-at-a-time in the "Addins" folder. If that's correct, could you change the AssembyResolve handler before you begin loading each addin, perhaps as a closure over the addin's folder name so that you know which addin you are adding when the handler gets called?
Paw - yep MEF is an option, but seems like overkill for a relatively simple task. While it wasn't exactly straight forward to arrive at this solution, it works and is easy to implement.
Donnie - while that might work I don't think that's a good idea. Assembly resolve errors can occur at any time, and you may end up with oddball dependencies getting pulled in that might have to be loaded differently or just passed back - too risky. I suppose one could track the current addin folder with a static variable and based on that know the active addin folder, but again it's not generic. The solution here - brute force as it is - works and it simply runs through files to find what it needs.
I had done a lot of this same kind of stuff quite some time ago when we were building the infamous Umbraco version 5. I wrote quite a bit about this, plus we had to support Med Trust which added a ton more complexity. A slight performance improvement could be gained by scanning the addins directories recursively and building a static dictionary of all the assemblies. That would at least put the disk cost to very close to the single directory solution.
Since you cannot unload dlls once loaded you can either shadow copy them app load option or ignore the uninstall after start issue. Then if you want a little bit of extra awesome you can add a file system watcher to rebuild your dictionary and load newly installed plugins.
I'm going to bookmark this for next time I need to update our LOB app that uses plugins. Always nice to pre-emptively prevent bugs. To answer your question of why the AssemblyResolve gets called even for assemblies already loaded, you need to dive into.
NET "Load Contexts". Assemblies loaded through Assembly. Another vote for MEF 2. MEF 1. MEF 2. My trajectory has been MEF, load assemblies from the same domain, from different appdomains and back to the same domain.
For me this is the most easy, not overkill and maintainable solution. Currently I prefer to use a mixed approach with a host that act as the addin assembly referencing the specific module as a nuget package. Rashidul - you cannot unload an assembly loaded into an appdomain. Once loaded always loaded until you unload the appdomain.
Unless you load assemblies explicitly into a separate AppDomain, they can never be unloaded. I had the same problem that. NET didn't find assemblies which were already loaded.. I suppose it's because I load from byte[] and paths are different thus dependencies are not found correctly.
But it's still strange since I have all dlls loaded manually using brute force. Anyway, the resolve callback fixed it. I'm very glad I saw your article in google search, it's difficult to find something a bit more advanced these days. Ad-free experience sponsored by:.
Share on:. On this page:. Is this content useful to you? Consider making a small donation to show your support. Posted in. Paw Baltzersen December 12, Donnie Hale December 12, I'm sure I'm missing something, but that's what went through my mind as I read your article.
As always, thanks for these. Rick Strahl December 13, Dustin December 13, Shannon Deminick December 13, The path parameter is null. The ability to execute code in remote assemblies is disabled. The path parameter is an empty string "" or does not exist. Version 2. Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths.
LoadFile does not load files into the load-from context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly. Starting with. NET Framework 4, if path specifies an assembly in a remote location, assembly loading is disabled by default, and the LoadFile method throws a FileLoadException.
This method is obsolete and will be removed in a future release of the. NET Framework. Please use an overload of LoadFile which does not take an Evidence parameter. The Common Language Runtime automatically deletes the files when they are no longer needed. Optionally set a custom location for shadow copied files by using the CachePath property and the ApplicationName property. The base path for the location is formed by concatenating the ApplicationName property to the CachePath property as a subdirectory.
Assemblies are shadow copied to subdirectories of this path, not to the base path itself. Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Tagged as C.
Stats Nikita Mazhara Rate me:. Please Sign up or sign in to vote. How to load assembly into memory in. NET application to have assembly file unlocked. Problem If you need to manipulate.
Using: C. Copy Code. Nikita Mazhara. Software Developer Quipu GmbH. My primary job is in development and maintainance of CutomWare. This is a large, flexible and constantly improving system which includes a huge set of modules for Front and Back-end, payment systems, loans, deposits and whatever other the Bank may need.
First Prev Next my time-saver Southmountain Apr Sean Feldman 3-Nov Sacha Barber 3-Nov Sacha Barber 2-Nov Nikita Mazhara 2-Nov Nikita Mazhara 4-Nov Sacha Barber 4-Nov SpArtA 2-Nov Nikita Mazhara 3-Nov SpArtA 3-Nov Go to top. Layout: fixed fluid. First Prev Next. Good work Sean Feldman 3-Nov Hi Nikita, You did a good work and don't feel bad that the same subject could have been handle in a million blog post or other ways.
0コメント