Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace WCF service library without restarting the service

I have a WCF service hosted in windows service and I there are 2 assemblies the host (.exe) and service library (.dll). When the service library is updated we have to stop the service in order to make it possible to replace the library. I would like to have IIS similar functionality, like to replace the library without restarting the service. Is it possible and how ?

like image 333
NDeveloper Avatar asked Jun 24 '11 14:06

NDeveloper


2 Answers

IIS uses something called a shadow copy to achieve this. You could implement something similar for your service host. Basically, the idea is that before starting the service, you copy the .DLL to some other location and have the host load your service class from that copy. The host then sets up a file system monitor to listen for changes to the original file. If it detects one, it stops the service, copies the new file, and restarts.

EDIT

(1) To start a ServiceHost using a class in a specific type library, you'd have to use reflection. Something like the following:

Assembly myAssembly = Assembly.LoadFile(path);

Type serviceType = myAssembly.GetType(className);

ServiceHost serviceHost = new ServiceHost(serviceType);

It's not clear from the documentation how LoadFile resolves dependencies. You may have to hook the Assembly.ModuleResolve event to make this work.

(2) File system monitors surely incur some overhead, but in my experience, it's minimal. In any case, this is really your only option unless you want to go with an installer for updated DLLs.

(3) I have no idea why your file is locked. You'll have to troubleshoot that yourself.

like image 165
Peter Ruderman Avatar answered Sep 27 '22 23:09

Peter Ruderman


Peter has one suggestion. Depending on the size and whether you can warrant it, another is to move your infrastructure to at least 2 clustered servers. This allows you to update one at a time, while the other(s) continue to take requests. As long as you version properly (contract changes == new method), this methodology works nicely, as older clients continue to get the same data regardless of your new bits.

like image 31
Gregory A Beamer Avatar answered Sep 28 '22 00:09

Gregory A Beamer