A complete solution for dynamic loading and unloading assembly

Goals

1: loaded dll distributed in different folders, you can no longer run the directory bin. In a way to create AppDomain load / unload
2: Run can automatically monitor the dll version, if dll and updates automatically uninstall the original dll, reload the new assembly (of course you have to have to be replaced with the running dll )
3: methods class loader can access a centralized main domain (main domain class instance of course can be accessed from the application domain)


advantage:

1: You can replace the concrete realization in operation, no need to stop the program before loading the
2: implement the program in a different domain isolation from each other, code runs relatively safe, a realization of a problem, you can uninstall only that one can AppDomain

 

several questions:

1: subroutine domain can not access the main domain configuration files, such as the app.config
2: Run speed will be slower, because of the need to repeatedly serialization / de-serialization.
3: debugging complex, debugging different application domains like recursive functions as difficult to debug
4: no longer pass an instance reference, all the way are passed by value.

Realization of ideas:

1: to load by creating different AppDomain dll, by unloading the AppDomain to unload DLL
2: through a Proxy class (inherited from MarshalByRefObject) to access specific implementation must not return an instance of a specific implementation, but rather by passing parameters in Acting in execution, and returns the result
3: implement methods to access the main domain through a ProxyBack (MarshalByRefObject) agents subroutine domain, this ProxyBack using TCP communication channel (in the Microsoft .Net Remoting)
4: All the different needs parameter passing between the domains / return value must be serializable.
5: Make a DLLWatcher to monitor the status of the folder where the dll


Implementation:

1: proxy class to create different ways to access routines in the domain of application domains
http://dlwang2002.cnblogs.com/archive/2005/10/18/257425.html

2: Use Microsoft .Net Remoting technology, making ProxyBack
on Microsoft .Net Remoting technology, Wayfarer's Prattle a very detailed explanation http://www.cnblogs.com/wayfarer/archive/2004/07/30/28723.html

Here mainly to see these major considerations and realize

This is Agent Server end, start the care program domain, listens
public ProxyBackServer ()
{
IF (ISSTART!)
{
The TcpChannel chan = new new the TcpChannel (8085);
ChannelServices.RegisterChannel (chan);

RemotingConfiguration.RegisterWellKnownServiceType (typeof (xxx. ProxyBackServer),
"CallLocalSM", WellKnownObjectMode.SingleCall);
ISSTART = to true;
}
method: Note delivered must be serializable objects also have to be loaded dll file in the folder assembly to achieve the object of this transfer .
public IStateDictionary RunService (IStateDictionary request)

In destructor Server in the TCP channel to be released

ProxyBackServer ~ ()
// {
// IChannel [] = ChannelServices.RegisteredChannels channels;
// // Close CallLocalSM name of the specified channel;
// the foreach (IChannel eachChannel in channels)
// {
// IF (= eachChannel.ChannelName = "CallLocalSM")
// {
// the TcpChannel TcpChannel = (the TcpChannel) eachChannel;
//
// // close monitor;
// tcpChannel.StopListening (null);
//
// // cancellation channel;
// ChannelServices.UnregisterChannel (TcpChannel);
//}
//}
//
//}

 

This is the Client-side proxy, the main program routine domain access domain when using
public ProxyBackClient ()
{
IF // Once Boot (isReg!)
{
The TcpChannel the TcpChannel Chan new new = ();
ChannelServices.RegisterChannel (Chan);
isReg to true = ;
}
_SERVER = (ProxyBackServer) the Activator.GetObject (
typeof (xxxx.ProxyBackServer), "TCP: // localhost: 8085 / CallLocalSM");
IF (_SERVER == null)
{
the throw new new Exception ( "CAN Not Connect to local Host GET instance of SM to ");
}

}
public IContext RunService(IContext ctx)
{
try
{
ctx.Response= this._server.RunService(ctx.Request);
return ctx;
..........

}

}

3:DllWatcher

............

= _MASK "* .dll";
// Process the initialize Watcher to Watch The Directory
this.dllWatcher the FileSystemWatcher new new = ();
this.dllWatcher .Path = _path;
this.dllWatcher .Filter = _MASK;
this.dllWatcher + = new new .Changed FileSystemEventHandler (File_OnChanged);
this.dllWatcher .Created + = new new FileSystemEventHandler (File_OnChanged);
this.dllWatcher .deleted + = new new FileSystemEventHandler (File_OnChanged);
// Tell IT to Start Watching
this.dllWatcher = .EnableRaisingEvents to true;
event is received after calling in the primary domain method to uninstall AppDomain, then you can reload

Guess you like

Origin www.cnblogs.com/blogpro/p/11458305.html