How to update dependency during runtime in the .NET application

client-serverdllnetsoftware-updates

I have a server-client application. Sometimes the server is updated which requires some DLLs in the client to be updated as well (The DLLs are the dependencies of the main executable). For now, we have to close the client, manually deploy the DLLs, and then start the client again. This is kind of an inconvenience because the client is an automated application, so normally it doesn't need any user intervention.

Is it possible for this to be done automatically without restart or user intervention? Like, the client would download the latest DLL, and replace the current one?

Best Answer

This is an ordinary sandbox model (the one which is used with plugins/addins). Instead of calling the libraries directly, you load them in a different AppDomain. Doing this actually allows you to update the corresponding libraries while the application is still running.

If you want to automate the process, the client application can monitor the directory with those libraries, and load newer ones to the sandboxed AppDomain when they are available.

Note that:

  • Using sandbox is not as easy as not using it, and you must be ready to have some issues. The two I had were that I was unable to put the UI elements in the sandboxed libraries and that cache stopped working when using sandbox.

  • If there are lots of exchange between the sandbox and the application itself, you may see the performance of your application decrease.

  • With a sandbox, you will not be able to unload, update and load the updated version of the library if the application is running at the same moment the code from this library. It's just not possible, and doesn't make sense.

    This is probably also why the browsers like Chrome and Firefox still need to restart to finish the updates.

    If this is your case, you may search for a way to indicate to the code from the library that it must stop, since the library will be unloaded.

  • The overall architecture of your application would be more complex, for sure. Does it worth it? Couldn't you schedule a background update, i.e. every night, if a new version is available, the application will just stop entirely, update itself (through an updater executable), and then be restarted by the updater?