You must fire the AssemblyResolve event when DisallowApplicationBaseProbing = true

I need to hook on the AssemblyResolve event in my created AppDomain when I set DisallowApplicationBaseProbing = true. The reason I do this is to force the runtime to raise the AssemblyResolve event needed to resolve the assembly, rather than trying it out first. Thus, another developer cannot simply insert MyDllName.dll into the ApplicationBase directory and override the assembly that I wanted to load in the AssemblyResolve event.

The problem with this is the following ...

class Program { static void Main() { AppDomainSetup ads = new AppDomainSetup(); ads.DisallowApplicationBaseProbing = true; AppDomain appDomain = AppDomain.CreateDomain("SomeDomain", null, ads); appDomain.AssemblyResolve += OnAssemblyResolve; appDomain.DoCallBack(target); } static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { Console.WriteLine("Hello"); return null; } private static void target() { Console.WriteLine(AppDomain.CurrentDomain); } } 

The code never skips the + = OnAssemblyResolve line.

When the code tries to execute, the new application domain tries to resolve the assembly in which I am running. Since DisallowApplicationBaseProbing = true, it does not know where to find this assembly. I seem to have a problem with the chicken and the egg. It must allow my build in order to connect the build tool, but a builder is needed to solve my build.

Thanks for any help.

-Mike

+3
c #
source share
1 answer

With a lot of experimentation, I got the following:

 internal class AssemblyResolver : MarshalByRefObject { static internal void Register(AppDomain domain) { AssemblyResolver resolver = domain.CreateInstanceFromAndUnwrap( Assembly.GetExecutingAssembly().Location, typeof(AssemblyResolver).FullName) as AssemblyResolver; resolver.RegisterDomain(domain); } private void RegisterDomain(AppDomain domain) { domain.AssemblyResolve += ResolveAssembly; domain.AssemblyLoad += LoadAssembly; } private Assembly ResolveAssembly(object sender, ResolveEventArgs args) { // implement assembly resolving here return null; } private void LoadAssembly(object sender, AssemblyLoadEventArgs args) { // implement assembly loading here } } 

The domain is created as follows:

  AppDomainSetup setupInfo = AppDomain.CurrentDomain.SetupInformation; setupInfo.DisallowApplicationBaseProbing = true; domain = AppDomain.CreateDomain("Domain name. ", null, setupInfo); AssemblyResolver.Register(domain); 

Sorry, I can’t share the code to allow and download assemblies. Firstly, it does not work yet. Secondly, in this context it will be too much and too specific to share with the general public.

I will load the object structure, which is serialized along with the assemblies required for deserialization from a single file. For this, I really deserve to add .dll directly.

+4
source share

All Articles