Imagine a scenario where our service system provides a series of functional services, and more new functional services will be added in the future, and functional services may need to be changed or removed. A common basic requirement for such a service system is that the operation of the service system cannot be stopped when adding/removing/updating functional services. Normally, "hot-swapping plugins" can be implemented very easily by encapsulating each service into a plugin dll, but "hot replacement" ("dynamic replacement") of plugins becomes a problem. The reason is that when we uninstall a plugin Dll from the service system, the service system actually still holds the underlying reference to the dll, and if you try to delete or overwrite the dll, windows will give a message like "the dll is being used".
So how to solve this problem? I think there are at least two options:
(1) Use AppDomain. Loading plugins in a new AppDomain and then uninstalling the AppDomain cleanly unloads the plugin dlls from the service system. The downside of this approach is that you need to manage many AppDomains (because you have so many functional services), and communication across AppDomains is done in a remoting way, which introduces a lot of unnecessary headaches to our system. If you're interested, you can also try this option, and I prefer to use the second one.
(2) Copy the plugin Dll in memory, and then load the Dll in memory. In this way, the DLLs on the hard drive can be overwritten or deleted at will. Previously we loaded plugin Dll directly from the hard drive, like this:
Now, we need to turn a corner:
This solves the problem of "dynamic replacement" of plugins.
|