Of course, the flip side of this is what is required to stop
a managed service. Fortunately, this is much simpler because we have already
done all the hard work. When a managed service assembly is deleted or when a
user chooses to stop the .NET Service Manager in the Services applet, a call is
made to ServiceBroker's StopService method (Listing 10). This method, as you
will see, takes a file path of the managed service to stop and uses that to
look up it up in the service names cache. If found, it will call the
UnloadService method, which we already covered in Listing 4, and then remove it
from the service names cache. Ah, if only it were all so simple!
Listing 10 – StopService
if (this.serviceNames.Contains(filePath))
{
this.UnloadService(Convert.ToString(this.serviceNames[filePath]));
this.serviceNames.Remove(filePath);
}
So now you've seen most of what's involved to give the .NET
Service Manager its magic. Obviously, most of this, especially the concepts
surrounding AppDomains, Remoting, and Reflection, could be very useful in any
application where you want to enable such cool features as dynamic loading,
updating, and unloading of assemblies.