How to use AppDomain/AppDomainSetup.PrivateBinPath ?

F

fostandy

Hi,

I am trying to load some assembly files into a new AppDomain at
runtime. I have gotten to the stage where I can copy the dll's to the
ApplicationBase directory and then load them. However when I copy them
to an AppicationBase/subdir directory, and then add this subdir to the
PrivateBinPath, I am not able to load them.

Some example code is below. Uncomment the line below "// LINE 45" for
a working version.

Can somebody tell me how I can make the PrivateBinPath directive work?
Or if this is not what it is supposed to do, then how do I go about
loading assembly .dll files that are NOT in the ApplicationBase
directory?

public class Class1
{

public static void Main(string [] args)
{
string BIN_PATH = "extrabin";

//uncomment to try fully qualified path
BIN_PATH = System.Environment.CurrentDirectory + "\\" +
BIN_PATH;

string ASSEMBLYFILE = @"c:\Temp\douylpo0.dll";

var ads = new AppDomainSetup();
ads.DisallowBindingRedirects = false;
ads.DisallowCodeDownload = true;
ads.ShadowCopyFiles = "true";
ads.ConfigurationFile =
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
ads.ApplicationBase =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
ads.PrivateBinPath = BIN_PATH;

if (!Directory.Exists(BIN_PATH))
Directory.CreateDirectory(BIN_PATH);

var scrDomain = AppDomain.CreateDomain("somedomain", null,
ads);

Console.WriteLine("Domain created, ApplicationBase is {3}
BaseDirectory is {0}, RelativeSearchPath is {0}, PrivateBinPath is
{2}", scrDomain.BaseDirectory, scrDomain.RelativeSearchPath,
scrDomain.SetupInformation.PrivateBinPath,
scrDomain.SetupInformation.ApplicationBase);

AssemblyLoader asmLoader =
(AssemblyLoader)
scrDomain.CreateInstanceAndUnwrap(typeof
(AssemblyLoader).Assembly.GetName().Name,
typeof
(AssemblyLoader).FullName);

FileInfo assemblyFile = new FileInfo(ASSEMBLYFILE);

// LINE 45
string destinationPath = BIN_PATH + "\\" +
assemblyFile.Name;
// string destinationPath = ".\\" + assemblyFile.Name;


if (File.Exists(destinationPath))
File.Delete(destinationPath);

File.Copy(ASSEMBLYFILE, destinationPath);

asmLoader.Load(AssemblyName.GetAssemblyName
(ASSEMBLYFILE).Name);

Console.Write("Successfully loaded!");

Console.ReadLine();



}


public class AssemblyLoader : MarshalByRefObject
{
public Assembly Load(string assemblyFile)
{
return AppDomain.CurrentDomain.Load
(assemblyFile);
// return System.Reflection.Assembly.Load
(assemblyFile);
}

public Assembly LoadFrom(string assemblyFile)
{
return Assembly.LoadFrom(assemblyFile);
// return System.Reflection.Assembly.LoadFrom
(assemblyFile);
}
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I am trying to load some assembly files into a new AppDomain at
runtime. I have gotten to the stage where I can copy the dll's to the
ApplicationBase directory and then load them. However when I copy them
to an AppicationBase/subdir directory, and then add this subdir to the
PrivateBinPath, I am not able to load them.

A little off topic , and I do apology for not directly answer your
question, but why you are loading it in another AppDomain? this in
general is slower than simply loading the dll in your current domain.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I am trying to load some assembly files into a new AppDomain at
runtime. I have gotten to the stage where I can copy the dll's to the
ApplicationBase directory and then load them. However when I copy them
to an AppicationBase/subdir directory, and then add this subdir to the
PrivateBinPath, I am not able to load them.

A little off topic , and I do apology for not directly answer your
question, but why you are loading it in another AppDomain? this in
general is slower than simply loading the dll in your current domain.
 
F

fostandy

A little off topic , and I do apology for not directly answer your
question, but why you are loading it in another AppDomain? this in
general is slower than simply loading the dll in your current domain.

No problem - it's basically because I want to be able to unload the
dll's later. This is part of a scripting framework/interface within my
main program. I want users to be able to build dll's and have them
loaded and executed. As this may be an iterative process I was hoping
to be able to avoid keeping the old dll's in memory as they are not
needed.

I realise that there's apparently a ~1000 factor slowdown with cross-
appdomain calls, so maybe I am optimising for the wrong thing?
 
F

fostandy

A little off topic , and I do apology for not directly answer your
question, but why you are loading it in another AppDomain? this in
general is slower than simply loading the dll in your current domain.

No problem - it's basically because I want to be able to unload the
dll's later. This is part of a scripting framework/interface within my
main program. I want users to be able to build dll's and have them
loaded and executed. As this may be an iterative process I was hoping
to be able to avoid keeping the old dll's in memory as they are not
needed.

I realise that there's apparently a ~1000 factor slowdown with cross-
appdomain calls, so maybe I am optimising for the wrong thing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top