CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Ranjan Sakalley


Assembly.Unload? Use AppDomain.Unload instead.

Well we ended up writing a VS.Net plugin to add to our developer testing tools (something like NUnitPlugin, but different). Not much, but almost the whole program was written and then there was this problem that all VS.Net plug-in writers face, atleast once in their life. Namely, you load an assembly, using Assembly.Load, but then how do you unload it? There is no Assembly.Unload out there ( Why isn’t there an Assembly.Unload method out there? ).

Solutions posted by Jason Zander, Suzanne Cook (if you haven’t read her blog, go read it, especially if you’re missing one Mr. Chris Brumme, and btw, Suzanne hasn’t posted for a long time too, so ping them and tell them that we want them back) mention that you need to load an assembly into a secondary appdomain, and then unload that appdomain. Eric Gunnerson’s MSDN article is a great help,here.

I will try to explain the pattern, with a console app that I wrote.

   The Type “Test” is the dynamic type we have, and even when the assembly containing this type is changed, the final console application should run the latest compilation. For simplicity, lets have a simple class that writes to console. Compile this and keep this in an assembly.

	public class Tested
	{
		public void Test()
		{
			Console.WriteLine("First Run");
		}
	}

First, create the assembly loader. The following code loads an assembly using a file path (hard coded here, you need to do it in a better way).
It gets the Type I wanted to run (Tested) and then invokes a method in the class ( “Test”) using an object of the Type. Create another assembly for this class.

 

          [Serializable]
	public class AssemblyLoader : MarshalByRef /*<updated>*/
	{
		public void LoadAndRun()
		{
			
			Assembly _assembly = Assembly.Load(<< assembly file path>>);
			Type _type =_assembly.GetType(“Tested”);
			MethodInfo _method =_type.GetMethod("Test");
			_method.Invoke(Activator.CreateInstance(_type),null);

		}
		
	}

 

Next create an AppDomainLoader(code below) (console exe here, you can have a class of your own in an assembly of your own) which creates an AppDomain. It then loads an instance of the assembly Loder class, using CreateInstanceAndUnwrap, which combines the CreateInstance and Object.Unwrap. It then runs the LoadAndRun method to indirectly invoke the latest version of the “Test” function in the “Tested” class.

 
	class AppDomainLoader
	{
		[STAThread]
		static void Main(string[] args)
		{
			while(Console.ReadLine() != "X")
			{
				try
				{
					Console.WriteLine("Press  to start");
					Console.ReadLine();
					AppDomain _domain = AppDomain.CreateDomain("Remote Load");
					AssemblyLoader _aLoader = 
						(AssemblyLoader)_domain.CreateInstanceAndUnwrap(
						"AssemblyLoader","Proteans.IPDev.PoC.AssemblyLoader ");
					
					_aLoader.LoadAndRun();

					AppDomain.Unload(_domain);
				}
				catch(Exception ex)
				{
					Console.WriteLine(ex.ToString());
				}
			}
		}

	}
Once you unload the app domain, the assembly automatically (well!) gets unloaded with it. I have written a while loop here,
which shall run the the process repetetively, till you want, and in between this, you can go ahead and recompile the “Tested”
class with a changed Console statement, and check if the latest changes are reflected in the output.
This can be a good skeleton to build on. Helpful?
 
 

Published Apr 08 2005, 05:16 AM by rsakalley
Filed under:

Comments

John Papa said:

Clever way to tackle this!
# April 8, 2005 5:43 AM

John Papa said:

// One typo, I believe you meant LoadFile:

Assembly _assembly = Assembly.Load(<< assembly file path>>);

// change this to

Assembly _assembly = Assembly.LoadFile(<< assembly file path>>);
# April 8, 2005 6:46 AM

Raymond Lewallen said:

Yet another great use for app domains! Awesome Ranjan!
# April 8, 2005 6:46 AM

rsakalley said:

John,
I had the file in the bin\debug directory there, yes you can use LoadFile too. I usually prefer this with evidence and full name though, which is not mentioned in the post, as it is just a skeleton.
# April 8, 2005 6:57 AM

rsakalley said:

John,
I undrstand the issue, and thats why there is an AssemblyLoader shim in the application. The question on Assembly.Unload has been answered by the aforementioned people already. Jason Zander has given out the problems related, and I am sure there is no solution out there other than this right now.
# April 10, 2005 10:20 PM

ahurwitz said:

if all you're worried about is being able to version and not have the app lock the assembly on disk, then you can just read the assembly into a buffer (byte array) and load it that way.
# January 5, 2006 1:18 PM

Henrik Erlandsson said:

How can you protect the callers code? For example, I would like to let the user call a remote function in a dll in his/her. That function must not expose my functions or properties. How can I ensure isolation of my program? I already have obfuscation etc built in but I want to prevent code extraction from in my program.

thanks
# April 5, 2006 8:55 AM

rsakalley said:

Henrik,

Obfuscation is not fool, proof, but it is probably the only way to hide the "code". Letting the users call your functions wouldn't be classified as code expose.
# April 10, 2006 4:19 PM

陈新波 said:

谢谢

# December 4, 2006 7:56 PM

NeVaL said:

http://www.l-italiano.beibi.info ^^^ http://www.l-karnitiini.beibi.info ^^^ http://www.rampete-penis-film.biseksuell.info ^^^ http://www.stygg-homofil-galleri.biseksuell.info ^^^ http://www.rampete-penis-film.erotiska.info ^^^ http://www.stygg-homofil-galleri.erotiska.info ^^^ http://www.klipp-prostituerad-striptease.fitta69.info ^^^ http://www.videor-fittor-harig.fitta69.info ^^^ http://www.gallerier-amator-mastubering.fotsex.info ^^^ http://www.nakna-heta-brud.fotsex.info ^^^ http://www.ujo-nuori-prostituoitu.isomuna.info ^^^ http://www.bb-alaston-ilmasto.isomuna.info ^^^ http://www.merkitykseton-biseksuaalinen.laukeaminen.info ^^^ http://www.sexsi-studio-ematin.laukeaminen.info ^^^ http://www.pornostjerne-stygg-porno.rype.info ^^^ http://www.thai-lolitas-portretter.rype.info ^^^ http://www.meloner-striptease-image.sadsprut.info ^^^ http://www.pen-kone-video.sadsprut.info ^^^ http://www.mpegit-jenter-orgasme.tytsy.info ^^^ http://www.mpegit-tuntemus-mallit.tytsy.info ^^^ http://www.anal-brunette-first.18analsex.com ^^^ http://www.galleries-big-asses.18analsex.com ^^^ http://www.download-horny-wifey.pazzesesso.com ^^^ http://www.racconto-ginecologo-filmato.pazzesesso.com ^^^ http://www.cameltoe-xxx-dvd.figanere.com ^^^ http://www.gif-simona-gnocca.figanere.com ^^^ http://www.orgia-masturbarsi-segarsi.inculatexxx.com ^^^ http://www.sex-anal-toy.inculatexxx.com ^^^ http://www.quarantenne-sexy-galleria.prostitutaculo.com ^^^ http://www.dvd-pornazzi-it.prostitutaculo.com ^^^ http://www.wrestling-facesitting-gif.lesbicastrip.com ^^^ http://www.lesbica-vecchia-gallerie.lesbicastrip.com ^^^ http://www.eroticos-cornudos-clips.007sexogratis.com ^^^ http://www.download-frase-erotica.007sexogratis.com ^^^ http://www.download-movies-reality.3sexogratis.com ^^^ http://www.orgia-paginas-cachondas.3sexogratis.com ^^^ http://www.japonesas-tetonas-fotos.analsexogratis.com ^^^ http://www.vedetes-pics.analsexogratis.com ^^^ http://www.avi-exportaciones-peru.cam-sexo-gratis.com ^^^ http://www.cuadros-galeria-jovencitas.cam-sexo-gratis.com ^^^

# December 28, 2006 5:54 AM

Smułko said:

http://www.ntex.nom.es/attractive-infermiera-masturbate.html

http://aventino-roma.vidot.com.es/

http://www.pipes.org.es/percepire-fighetta-fotti.html

http://www.ntex.nom.es/hotel-albergo-firenze.html

http://www.dfms.org.es/8eel2e2ni.html

http://b72df4qms.arteg.nom.es/

http://www.pipes.org.es/piu-bollente-sborrata.html

http://www.iolao.nom.es/152655668/

http://150396756.dmdaa.org.es/

http://rgh3k533m.arteg.nom.es/

http://www.ntex.nom.es/ph-meter.html

http://www.ntex.nom.es/gola-sexi-zene.html

http://comfortable-tedesco-pompino.vidot.com.es/

http://www.ntex.nom.es/penna-usb-mp3.html

http://www.fescu.nom.es/segretarie-in-minigonna/

http://www.worri.org.es/3xhvhk7l6.html

http://www.fescu.nom.es/amichevole-amatoriali-schizzate-di-figa.htmlhttp://www.dewo9m907by.info/sexo-fighette-ubriache.html

http://freddissimo-schizzate-di-figa.dewo9m907by.info/

http://last-minute-danimarca.dhffimsbmup.info/

http://pezzo-di-bionda.1xl80kcb2o6.info/

http://posti-it.t315o15wuo0.info/

http://www.k3pfcr5x03e.info/tv-lcd-30.html

http://derisorio-ragazze-urinate.101qprrep2h.info/

http://pornoactress.u4re8o6n1qf.info/

http://strano-dominazione.1xl80kcb2o6.info/

http://viaggi-ventura.1okkjnwrl2i.info/

http://congenial-infermiera-urinate.101qprrep2h.info/

http://monza-albergo-hotel.5evij5hitsj.info/

http://www.nupsvtr1xzi.info/146070072/

http://donne-mature-film.dewo9m907by.info/

http://cartuccia-michelangelo.o8nr6j39nzr.info/

http://foto-di-nudo-amatoriale.q88p8bre85g.info/

http://cameriera-inculate-nellappartamento.3wcekxwg3md.info/

http://secondapelle-it20060228vedere-e-potere-respond.u4re8o6n1qf.info/

http://ritirarsi-fighette-anale-fotti.79pkaqzrpg2.info/

http://prenotazione-hotel-meta.37of3kjgtmf.info/

# January 24, 2007 1:11 AM

hans said:

http://www.xmeup.com.es/cavalli-e-ssex.html :!:

http://sex-mangia-sborra.cosat.nom.es/ :!:

http://geisha-trailer.maxc.org.es/ :!:

http://www.quest.org.es/yprh2l1u.html :!:

http://www.do-is.org.es/144031188.html :!:

http://low-cost-viaggi.fianc.nom.es/ :!:

http://famoso-infermiera-sesso.dget.org.es/ :!:

http://sms-picture-message.liss.org.es/ :!:

http://sex-piesex.agen.com.es/ :!:

http://www.dget.org.es/149187540/ :!:

http://habitat-new-york.maxc.org.es/ :!:

http://suoneria-composite-samsung.xmeup.com.es/ :!:

http://fun-fighette-fottilo.arik.org.es/ :!:

http://saldi-moda.xmeup.com.es/ :!:

http://wind-5-centesimo.dolo.com.es/ :!:

http://www.quest.org.es/btpmpsr661.html :!:

http://www.porgi.nom.es/3epeews2l.html :!:

http://www.vale.com.es/tqlbubjx.html :!:

http://online-banking.fianc.nom.es/ :!:

http://www.e-bcs.org.es/sdylj4hng/ :!:

http://timido-lesbiche-masturbate.fianc.nom.es/ :!:

http://www.zoso.nom.es/sicilia-villaggio-mare.html :!:

http://www.hpcds.nom.es/orientamento-formazione/ :!:

http://lust-asiatiche-ubriache.tcra.org.es/ :!:

http://comprensivo-giovane-spogliarello.toxot.org.es/ :!:

http://giovane-grande-cappella.pcode.com.es/ :!:

http://hotel-bolzano-centro.tudoc.org.es/ :!:

http://congeniale-fighetta-ubriache.tcra.org.es/ :!:

http://roberta-cavalcante.bjerg.nom.es/ :!:

http://nonne-arrapate.pcode.com.es/ :!:

http://hotel-mediterraneo-siracusa.cosat.nom.es/ :!:

http://lavoro-offerta-pisa.apwap.org.es/ :!:

http://produzione-vendita-materasso.toxot.org.es/ :!:

http://www.vale.com.es/oplms6ow.html :!:

http://www.arpne.org.es/ctnum2xuvq.html :!:

http://rosse-troie.amgoo.nom.es/ :!:

http://www.zoso.nom.es/allesterno-cinema.html :!:

http://napoli-ospedali.amgoo.nom.es/ :!:

http://www.iajce.nom.es/divertente-lesbiche-doppio-penetrazione.html :!:

http://www.crios.org.es/lnnj5n4ziq.html :!:

http://ricerca-volo-economico.veras.nom.es/ :!:

http://www.maxc.org.es/airline-ticket/ :!:

http://verona-scalzi.digo.nom.es/ :!:

http://comprare-pneumatico-online.arpne.org.es/ :!:

http://annuncio-scambio-toscana.pcode.com.es/ :!:

http://grazioso-fighette-merda.amgoo.nom.es/ :!:

http://polaroid-camera.vale.com.es/ :!:

http://www.pcode.com.es/annuncio-girl-brescia.html :!:

http://www.pcode.com.es/cazzi-culo-e-figa-spacca-spacca.html :!:

http://freddissimo-assurdo-figlia.porgi.nom.es/ :!:

http://feet-torture.fianc.nom.es/ :!:

# January 27, 2007 9:54 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors