dll loading

D

DaveL

I am Trying to load a Dll from any folder
How to do this...

I can get it loaded and execute the instance or static methods when its in
the gac or with the application

i want to load it from any folder, with out interfaces
DaveL
 
P

Pavel Minaev

I am Trying to load a Dll from any folder
How to do this...

I can get it loaded and execute the instance or static methods when its in
the gac or with the application

i want to load it from any folder, with out interfaces

I have no idea what you mean by "with out interfaces", but if you want
to load an assembly from a specific path that you only know at run-
time, you can use Assembly.LoadFrom().
 
D

DaveL

Assembly.LoadFrom loads into the Appdomain
i need it loaded in the secondary Domain
so i can unload it and load other dlls based on
different customer needs



DaveL

I am Trying to load a Dll from any folder
How to do this...

I can get it loaded and execute the instance or static methods when its in
the gac or with the application

i want to load it from any folder, with out interfaces

I have no idea what you mean by "with out interfaces", but if you want
to load an assembly from a specific path that you only know at run-
time, you can use Assembly.LoadFrom().
 
P

Pavel Minaev

Assembly.LoadFrom loads into the Appdomain
i need it loaded in the secondary Domain
so i can unload it and load other dlls based on
different customer needs

LoadFrom loads into the _current_ AppDomain. So create a second
AppDomain, and call LoadFrom in that one.

Of course, you will have to deal with all the cross-domain marshalling
problems this entails; but this has nothing to do with loading
assemblies as such.
 
D

DaveL

Thats what im having Problems with....
Most Examples ive found , there all loading into the
current Domain....

ive used mySecondDomain.Load(DllName) -> failes when not AppDomain folder or
gac
also ive used byets[] i get the same file not found error

DaveL




Assembly.LoadFrom loads into the Appdomain
i need it loaded in the secondary Domain
so i can unload it and load other dlls based on
different customer needs

LoadFrom loads into the _current_ AppDomain. So create a second
AppDomain, and call LoadFrom in that one.

Of course, you will have to deal with all the cross-domain marshalling
problems this entails; but this has nothing to do with loading
assemblies as such.
 
P

Pavel Minaev

Thats what im having Problems with....
Most Examples ive found , there all loading into the
current Domain....

ive used mySecondDomain.Load(DllName) -> failes when not AppDomain folderor
gac
also ive used byets[] i get the same  file not found error

AppDomain.Load(), just like Assembly.Load(), doesn't take a file name
as an argument. It takes a strong assembly name as an argument.
However, you can still initiate a call Assembly.LoadFrom - which does
what you want - from the first appdomain, but within the second
appdomain, via AppDomain.DoCallBack. E.g.:

AppDomain other = ...;
other.DoCallBack(
delegate
{
Assembly.LoadFrom("C:\\foobar.dll");
...
});

Now, most likely you need to do more than to just load assembly in
another domain - like, say, using it by instantiating types and
calling methods :) In this case, use serializable types and
MarshalByRefObject-derived classes as needed to pass data back and
forth. If you have any _specific_ questions regarding the latter, then
you are welcome to ask - but please be specific with what exactly
you're trying to achieve.
 
D

DaveL

Thanks alot Pavel much appriciated
below is some test code , i know its not perfect just testing stuff



AssemblyName AsmName = new AssemblyName();
AsmName.Name = "cstest";
AsmName.CodeBase = @"d:\cstest";

AppDomainSetup DomainSetup = new AppDomainSetup();
DomainSetup.ApplicationBase =
AppDomain.CurrentDomain.BaseDirectory; // "d:\\cstest";
DomainSetup.DisallowApplicationBaseProbing = false;
DomainSetup.PrivateBinPath = DomainSetup.ApplicationBase
+ ";d:\\cstest;" + DomainSetup.ApplicationBase + "\\scripts";

DomainSetup.ApplicationName = AsmName.FullName;

AppDomain newDomain = AppDomain.CreateDomain("myDomain",
null, DomainSetup);




Assembly asm = null;
bool bStatic = true;
asm = LoadAssembly(ref newDomain);

System.Type MyType = asm.GetType("TestScript.Program");

object[] Params = new object[1] { "hello world" };
if (bStatic == false)
{
MyType = asm.GetType("TestScript.StringTest");
MethodInfo Mi = MyType.GetMethod("SayString");
//MyType=Mi.GetType();
Object o = Activator.CreateInstance(MyType);
Mi.Invoke(o, Params);
}
else
{
MyType = asm.GetType("TestScript.Program");
MyType.InvokeMember("Main",
BindingFlags.InvokeMethod, null, null, new object[0]);
}

AppDomain.Unload(newDomain);
Console.WriteLine("domain Unloaded");



}
catch (Exception ex)
{
NetTools.dvErrorSysWin.ShowError(ex);
}


}

public static Assembly LoadAssembly(ref AppDomain oDomain)
{
//havent been able to load from other folders other than the
Host Exe folder
//but still being loaded into the Appdomain not the
SecondaryDomain
//i'll keep working at it

Assembly asm = null;
System.IO.File.Copy("D:\\cstest\\csTest.dll","d:\\bin\\cstest.dll",true);
try
{
asm = oDomain.Load("cstest");
}
catch( Exception ex)
{
NetTools.dvErrorSysWin.ShowError(ex);
}
return asm;

}

}

}

Thats what im having Problems with....
Most Examples ive found , there all loading into the
current Domain....

ive used mySecondDomain.Load(DllName) -> failes when not AppDomain folder
or
gac
also ive used byets[] i get the same file not found error

AppDomain.Load(), just like Assembly.Load(), doesn't take a file name
as an argument. It takes a strong assembly name as an argument.
However, you can still initiate a call Assembly.LoadFrom - which does
what you want - from the first appdomain, but within the second
appdomain, via AppDomain.DoCallBack. E.g.:

AppDomain other = ...;
other.DoCallBack(
delegate
{
Assembly.LoadFrom("C:\\foobar.dll");
...
});

Now, most likely you need to do more than to just load assembly in
another domain - like, say, using it by instantiating types and
calling methods :) In this case, use serializable types and
MarshalByRefObject-derived classes as needed to pass data back and
forth. If you have any _specific_ questions regarding the latter, then
you are welcome to ask - but please be specific with what exactly
you're trying to achieve.
 
D

DaveL

Pavel... The Callback did the trick

I thank you very very much
DaveL

Thats what im having Problems with....
Most Examples ive found , there all loading into the
current Domain....

ive used mySecondDomain.Load(DllName) -> failes when not AppDomain folder
or
gac
also ive used byets[] i get the same file not found error

AppDomain.Load(), just like Assembly.Load(), doesn't take a file name
as an argument. It takes a strong assembly name as an argument.
However, you can still initiate a call Assembly.LoadFrom - which does
what you want - from the first appdomain, but within the second
appdomain, via AppDomain.DoCallBack. E.g.:

AppDomain other = ...;
other.DoCallBack(
delegate
{
Assembly.LoadFrom("C:\\foobar.dll");
...
});

Now, most likely you need to do more than to just load assembly in
another domain - like, say, using it by instantiating types and
calling methods :) In this case, use serializable types and
MarshalByRefObject-derived classes as needed to pass data back and
forth. If you have any _specific_ questions regarding the latter, then
you are welcome to ask - but please be specific with what exactly
you're trying to achieve.
 

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

Similar Threads

loading dll at runtime 5
Threading 3
Global Assembly Cache 7
DLL loading Question 5
about adding a assembly dll to the GAC 2
XML file vs. DLL? 2
loading dll dynamically 3
Dynamic loading - My first LINQ sentence 5

Top