Goals
1: The loaded DLL files are distributed in different folders. You can stop running the directory bin. Load/uninstall by creating an appdomain
2: The dll version can be automatically monitored during running. If the DLL is updated, the original DLL is automatically uninstalled and the new one is reloaded.ProgramSet (of course, you must be able to replace the running DLL)
3: load the class in the Assembly to access the main program domain (the class in the main program domain can certainly access the instance in the Self-program domain)
Advantages:
1: You can replace the specific implementation in the running without stopping the program and then loading it.
2: implementations in different program domains are isolated from each other,CodeThe operation is safe. If an implementation fails, you can just uninstall that appdomain.
Several questions:
1: Some configuration files in the main program domain cannot be accessed in the subroutine domain, such as app. config.
2: The running speed is slower, because repeated serialization/deserialization is required.
3: debugging is complex. debugging of different program domains is like a recursive function, which is difficult to debug.
4: The instance reference is no longer passed, and all values are passed.
Implementation:
1: create different AppDomains to load the DLL, and uninstall the DLL by detaching the appdomain.
2: Use a proxy class (inherited fromMarshalbyrefobject).
3: A proxyback (Marshalbyrefobject) To access the methods in the main program domain. The proxyback uses the TCP channel for communication (Microsoft. NET remoting)
4: All parameters/return values that need to be passed between different domains must be serializable.
5: Create a dllwatcher to monitor the status of the DLL folder.
Implementation:
1: The proxy class creates methods in different program domain access subroutine Domains
Http://dlwang2002.cnblogs.com/archive/2005/10/18/257425.html
2: Use Microsoft. NET remoting technology to create proxyback
About Microsoft. NET remoting technology, Wayfarer's prattle has a very detailed explanation of http://www.cnblogs.com/wayfarer/archive/2004/07/30/28723.html
Here, we will mainly look at the main implementation and precautions.
This is the proxy server. It cares about startup and listening in the program domain.
Public proxybackserver ()
{
If (! Isstart)
{
Tcpchannel Chan = new tcpchannels (8085 );
Channelservices. registerchannel (Chan );
Remotingconfiguration. registerwellknownservicetype (typeof (XXX. proxybackserver ),
"Calllocalsm", wellknownobjectmode. singlecall );
Isstart = true;
}
Method: note that all passed objects must be serializable objects, and the Assembly implemented by the passing object must be included in the folder where DLL needs to be loaded.
Public istatedictionary runservice (istatedictionary request)
Release the TCP channel in the server's destructor
~ Proxybackserver ()
// {
// ichannel [] channels = channelservices. registeredchannels;
/// close the channel named calllocalsm;
// foreach (ichannel eachchannel in channels)
// {
// If (eachchannel. channelname = "calllocalsm")
// {
// tcpchannel = (tcpchannel) eachchannel;
//
/// disable the listener.
// tcpchannel. stoplistening (null);
// logout channel;
// channelservices. unregisterchannel (tcpchannel);
//}< BR >/}< BR >//
///}
This is the proxy client. When the subroutine domain accesses the main program domain, use
Public proxybackclient ()
{< br> If (! Isreg) // Boot once
{< br> tcpchannel Chan = new tcpchannel ();
channelservices. registerchannel (Chan);
isreg = true;
}< br> _ Server = (proxybackserver) activator. getObject (
typeof (XXXX. proxybackserver), "TCP: // localhost: 8085/calllocalsm");
If (_ Server = NULL)
{< br> throw new exception ("can not connect to local host to get instance of SM");
}
}
Public icontext runservice (icontext CTX)
{
Try
{
CTX. Response = This. _ server. runservice (CTX. request );
Return CTX;
..........
}
}
3: dllwatcher
............
_ Mask = "*. dll ";
// Initialize watcher to watch the process directory
This. dllwatcher = new filesystemwatcher ();
This. dllwatcher. Path = _ path;
This. dllwatcher. Filter = _ mask;
This. dllwatcher. Changed + = new filesystemeventhandler (file_onchanged );
This. dllwatcher. Created + = new filesystemeventhandler (file_onchanged );
This. dllwatcher. Deleted + = new filesystemeventhandler (file_onchanged );
// Tell it to start watching
This. dllwatcher. enableraisingevents = true;
After receiving the event, call the method in the main domain to uninstall the appdomain and reload it.