Search code examples
c#serviceappdomainappdomainsetup

Configure Shadow Copy using app.config


Let me explain the scenario first.

I have installed multiple copies (Say 10) of Services from a single install base. Now I want to update one of the dll. I need to Stop all the services, update the dll and restart the Service again.

To avoid the situation, I used ShadowCopying in code. So that the dlls can be updated without stopping all the services. It is as follows.

static void Main(string[] args)
{
    AppDomain.CurrentDomain.SetCachePath(@"C:\Cache");
    AppDomain.CurrentDomain.SetShadowCopyPath(AppDomain.CurrentDomain.BaseDirectory);
    AppDomain.CurrentDomain.SetShadowCopyFiles();

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
        { 
            new SampleService(serviceName) 
        };
    ServiceBase.Run(ServicesToRun);
}

Now I am trying to achieve the same via app.config file, as follows, from Asp.Net

<hostingEnvironment 
    idleTimeout="Infinite" 
    shutdownTimeout="30" 
    shadowCopyBinAssemblies="true" />

Any suggestions?


Solution

  • The ASP.Net hosting environment has built-in support for managing application recycling.

    Windows .Net services use the standard CLR host which does not have this support. You would have to implement your own e.g.

    1. Create a child AppDomain to host your service code, and configure shadow copying.
    2. Use something like a FileSystemWatcher to monitor the original bin directory.
    3. When files change, tear down your AppDomain and create a new one and reload.

    The ASP.Net host does something along these lines (but also has the ability to manage existing requests, queue new requests whilst this is going on, etc).