Using LibZ with new application domain

Jul 15, 2014 at 2:04 PM
I'm creating a new AppDomain to accomplish a certain task in my application. All the dlls are injected into one main dll. This main dll is used in classic ASP pages (as a COM objects library).

LibZ Assembly resolver is probably not used in this new AppDomain. I receive
System.IO.FileNotFoundException: Could not load file or assembly 'XX....'
errors when creating new objects in this domain via CreateInstanceAndUnwrap.
I can make it work when I either put the required dlls into GAC or into a probe path, which are both unwanted in the production.

Is there any way how to make LibZ.Injected.AsmZResolver work in this new domain, too?
Coordinator
Jul 15, 2014 at 2:50 PM
You are right. AssemblyResolver works because it handles: "AppDomain.CurrentDomain.AssemblyResolve" and, as you can guess, CurrentDomain means Current not New.

This is, kind of, advanced scenario and is not handled out-of-the-box. Although, you could achieve what you want by using embedded .libz container (Scenario 4) and no-instrumentation approach (Scenario 6). See this.
Jul 23, 2014 at 3:37 PM
Edited Jul 23, 2014 at 3:38 PM
Thank you, it works like a charm.

Firstly, I create and embed .libz container into the main dll.
Then I create a new helper class:
[Serializable]
internal class LibZAppDomainHelper : MarshalByRefObject
{
    internal static void InitializeLibZFileContainer(Action startupAction = null)
    {
        LibZ.Bootstrap.LibZResolver.RegisterFileContainer("main.dll");
        if ( null != startupAction )
            LibZ.Bootstrap.LibZResolver.Startup(startupAction);
    }

    public LibZAppDomainHelper()
    {
        LibZAppDomainHelper.InitializeLibZFileContainer();
    }
}
This class has 2 usages:
  1. When the application starts, I call
LibZAppDomainHelper.InitializeLibZFileContainer(() =>
{
    ... // application startup
});
  1. When I create a new AppDomain, I create an instance of this class first.
Type myType = typeof(LibZAppDomainHelper);
string assemblyName = myType.Assembly.FullName;
... // AppDomain evidence and setup
AppDomain appDomain = AppDomain.CreateDomain("LibZAppDomainHelper.AppDomain", evidence, setup);

appDomain.CreateInstanceAndUnwrap(assemblyName, myType.FullName);
... // do the other stuff in appDomain