Reputation: 2966
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);
}
Upvotes: 3
Views: 5640
Reputation: 15794
The AssemblyResolve
will never fire, because you're loading up all the assemblies in the LoadApplication
method - AssemblyResolve
will 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:
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();
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.
Upvotes: 3
Reputation: 1817
AssemblyResolve fill fire only of CLR is failed to load an assembly by one of the reasons. This event will not fire just because you loading your assemblies from byte array.
Here are some examples.
This code will fire AssemblyResolve event, because "System.Drawing" as an assembly name is not sufficient.
object obj2 = ApplicationHost.CreateInstanceAndUnwrap("System.Drawing", "System.Drawing.Rectangle");
This code will not fire AssemblyResolve event, because "System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" is a fully qualified assembly name.
object obj2 = ApplicationHost.CreateInstanceAndUnwrap("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Rectangle");
Upvotes: 2