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 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);
}
}
}