Catch dll loading
I am trying to build a .NET client software package that downloads its components on demand.
Let's say I have a program that is split into a main executable and 20 other dll files. The main program references 3 of them, they reference some others, anyway... they have some kind of tree like dependency structure.
The thing I am trying to achieve is to distribute just the main executable and get everything else from a server location on-demand.
Something like this: The main program and all these dll projects are in a single solution and are built tog开发者_如何学Goether just like any other solution. While distributing, only the exe is distributed, the other dll's (including some third party libraries used) are put in a server location available for download.
The exe runs, shows some UI, when the user clicks a menu item, another UI window from one the dll files is to be shown so the OS looks for the dll (which is not there), I intervene, download the required dll from the server, put it next to the exe and let the OS load it as if it was there from the very beginning.
This looks achievable by the use of a common Interface class and some reflection magic but I was hoping for something more, something that includes building the dll's altogether in a single solution, something that includes the on-demand download of 3rd party libraries.
Any ideas how to do this?
You're looking for the AppDomain.AssemblyResolve
event, which allows you to load assemblies from custom locations.
You won't need any other reflection.
Provide event handlers for AppDomain.ResolveAssembly and AppDomain.ResolveType events for your current appdomain and load the assemblies.
But be careful to load the assemblies into the right context: http://msdn.microsoft.com/en-us/library/dd153782.aspx
You can cath the AssemblyResolve event, that fires when an assembly is not found, and then download the assembly it tries to find.
You should read this article.
精彩评论