i really wonder why assemblyResolver not working? Also i can not use
foreach (byte[] binary in deCompressBinaries)
ApplicationHost.Load(binary);
how to fire AssemblyResolve? please look my reference question: http://stackoverflow.com/questions/9721686/how-to-use-appdomain-createdomain-with-assemblyresolve
protected void LoadApplication()
{
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
domainSetup.DisallowBindingRedirects = false;
domainSetup.DisallowCodeDownload = true;
domainSetup.LoaderOptimization = LoaderOptimization.SingleDomain;
//domainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
ApplicationHost = AppDomain.CreateDomain("Test.Service", null, domainSetup);
object obj = ApplicationHost.CreateInstanceAndUnwrap("Test.Process", "Test.ApplicationLoader");
Assembly objExecutingAssemblies = Assembly.GetExecutingAssembly();
AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
foreach (AssemblyName assName in arrReferencedAssmbNames)
{
ApplicationHost.Load(assName);
}
ApplicationHost.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
List<byte[]> deCompressBinaries = new List<byte[]>();
foreach (var item in AppPackage.Item.AssemblyPackage)
deCompressBinaries.Add(item.Buffer);
var decompressvalues = DeCompress(deCompressBinaries);
deCompressBinaries.Clear();
deCompressBinaries = decompressvalues.ToList();
foreach (byte[] binary in deCompressBinaries)
ApplicationHost.Load(binary);
Assembly[] assAfter = AppDomain.CurrentDomain.GetAssemblies();
}
Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
return Assembly.Load(args.Name);
}
The
AssemblyResolvewill never fire, because you’re loading up all the assemblies in theLoadApplicationmethod –AssemblyResolvewill only be called if the required assembly reference fails to be resolved.I would suggest running fuslogvw.exe so that you can visualize what is going on.
If you want to load the assemblies on an as-needed basis, the block of code:
will need to be worked into the
AssemblyResolve. The block currently loads EVERYTHING into the AppDomain, so you’d have to rework that piece of logic.Of course, the other thing you might want do is not to re-invent the wheel, and use ILMerge.