Replace WCF service library without restarting the service

I have a WCF service hosted in a Windows service, and I have 2 host assemblies (.exe) and a service library (DLL). When the service library is updated, we must stop the service so that the library can be replaced. I would like to have similar IIS functionality, for example, replace the library without restarting the service. Is it possible and how?

+6
source share
2 answers

To achieve this, IIS uses something called a shadow copy. You can implement something similar for your host. Basically, the idea is that before starting the service, you copy .DLL to another location and load your service class from this copy. The host then installs a file system monitor to listen for changes to the source file. If it discovers one, it stops the service, copies the new file, and restarts.

EDIT

(1) To start ServiceHost using a class in a library of a certain type, you will need to use reflection. Something like the following:

Assembly myAssembly = Assembly.LoadFile(path); Type serviceType = myAssembly.GetType(className); ServiceHost serviceHost = new ServiceHost(serviceType); 

The documentation does not show how LoadFile resolves dependencies. You may need to hook up the Assembly.ModuleResolve event to make this work.

(2) File system monitors are likely to incur some overhead, but in my experience they are minimal. In any case, this is really your only option if you do not want to go with the installer for the updated DLLs.

(3) I do not know why your file is locked. You will have to fix this problem yourself.

+4
source

Peter has one suggestion. Depending on the size and whether you can guarantee it, the other is to move the infrastructure to at least 2 cluster servers. This allows you to update one at a time, while others (s) continue to receive requests. As long as you execute the version correctly (the contract changes == the new method), this methodology works well, as old clients continue to receive the same data regardless of your new bits.

+2
source

Source: https://habr.com/ru/post/891294/


All Articles