loading dll at runtime

D

DaveL

Im looking for some good links
to show me how to load and unload dlls at runtime
specifically

I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain

Thanks
DaveL
 
M

Morten Wennevik [C# MVP]

Hi Dave,

After fidling with some code I think I got a workable solution, although it
may not be the most elegant.

The code below assumes there is a Data.dll in a nearby folder, and loads it
into a subdomain. The trick is that AppDomain.Load requires assembly names
or similar instead of the exact file name, so you may need to load it using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and obtain
its FullName first. Having the assembly name you can tell the sub domain to
look anywhere on the disk for the file, and load it.

protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";

AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);

subDomain.Load(assemblyName);

Assembly[] assemblies = subDomain.GetAssemblies();

Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;

Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);

AppDomain.Unload(subDomain);
}

Once the subdomain is unloaded the assembly file is no longer locked even
though the CurrentDomain is still running.
 
D

DaveL

Thanks alot...Morten

that is heading in the right direction
but what i understand ..once a dll is loaded in the current domain it can
not be unloaded....
its possible i can have 500 one off .cs files these Represent custom
flexible code for customer needs
each dll would have a lets say static start() method
so the calling application sees the Same Dll and method for each
customer...even though the internal work they will do can and usually
different then other customers

I will work with you example...
I guess i need to find a way to get Assembly info b4 i load it into the
secondary app domain..

Isn't it possible if I have to load it in the current domain to get
assembly info, that i could have a ton of signatures after processing, since
this will be a service and running 24x7

Currently i use xsl to do the customer specific..but the files can be pretty
large..
So i would rather use cs Files which give me the power of compiled code and
flexibility to do any thing .net Can do

Thanks alot
DaveL
Morten Wennevik said:
Hi Dave,

After fidling with some code I think I got a workable solution, although
it
may not be the most elegant.

The code below assumes there is a Data.dll in a nearby folder, and loads
it
into a subdomain. The trick is that AppDomain.Load requires assembly
names
or similar instead of the exact file name, so you may need to load it
using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and obtain
its FullName first. Having the assembly name you can tell the sub domain
to
look anywhere on the disk for the file, and load it.

protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";

AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);

subDomain.Load(assemblyName);

Assembly[] assemblies = subDomain.GetAssemblies();

Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;

Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);

AppDomain.Unload(subDomain);
}

Once the subdomain is unloaded the assembly file is no longer locked even
though the CurrentDomain is still running.

--
Happy Coding!
Morten Wennevik [C# MVP]


DaveL said:
Im looking for some good links
to show me how to load and unload dlls at runtime
specifically

I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain

Thanks
DaveL
 
M

Morten Wennevik [C# MVP]

They should all be unloaded when the subdomain unloads. I only used
Assembly.LoadFile in the CurrentDomain to obtain the FullName from my
Data.dll. I'm afraid you can't use this method to obtain the assembly
fullname at runtime, as once the assembly is loaded it will remain so until
the domain unloads, which basically means the application shutdown if you
load it into the main domain. There may be other ways to obtain the FullName
though. Besides the FullName is fairly simple if the assembly is not signed
(publicKey=null).

--
Happy Coding!
Morten Wennevik [C# MVP]


DaveL said:
Thanks alot...Morten

that is heading in the right direction
but what i understand ..once a dll is loaded in the current domain it can
not be unloaded....
its possible i can have 500 one off .cs files these Represent custom
flexible code for customer needs
each dll would have a lets say static start() method
so the calling application sees the Same Dll and method for each
customer...even though the internal work they will do can and usually
different then other customers

I will work with you example...
I guess i need to find a way to get Assembly info b4 i load it into the
secondary app domain..

Isn't it possible if I have to load it in the current domain to get
assembly info, that i could have a ton of signatures after processing, since
this will be a service and running 24x7

Currently i use xsl to do the customer specific..but the files can be pretty
large..
So i would rather use cs Files which give me the power of compiled code and
flexibility to do any thing .net Can do

Thanks alot
DaveL
Morten Wennevik said:
Hi Dave,

After fidling with some code I think I got a workable solution, although
it
may not be the most elegant.

The code below assumes there is a Data.dll in a nearby folder, and loads
it
into a subdomain. The trick is that AppDomain.Load requires assembly
names
or similar instead of the exact file name, so you may need to load it
using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and obtain
its FullName first. Having the assembly name you can tell the sub domain
to
look anywhere on the disk for the file, and load it.

protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";

AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);

subDomain.Load(assemblyName);

Assembly[] assemblies = subDomain.GetAssemblies();

Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;

Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);

AppDomain.Unload(subDomain);
}

Once the subdomain is unloaded the assembly file is no longer locked even
though the CurrentDomain is still running.

--
Happy Coding!
Morten Wennevik [C# MVP]


DaveL said:
Im looking for some good links
to show me how to load and unload dlls at runtime
specifically

I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain

Thanks
DaveL
 
D

DaveL

Thanks morten
I think were on the same page.....
Im gonna spend a few hourse today, doing different tests
to get the assembly name.....

Pretty much ive writen my code like your example...
Im at the stumbling block getting the Assembly name

I wasn't sure the direction i should continue...so you have put me in the
right direction
Thanks DaveL

I'll post here later let you know my findings

DaveL




Morten Wennevik said:
They should all be unloaded when the subdomain unloads. I only used
Assembly.LoadFile in the CurrentDomain to obtain the FullName from my
Data.dll. I'm afraid you can't use this method to obtain the assembly
fullname at runtime, as once the assembly is loaded it will remain so
until
the domain unloads, which basically means the application shutdown if you
load it into the main domain. There may be other ways to obtain the
FullName
though. Besides the FullName is fairly simple if the assembly is not
signed
(publicKey=null).

--
Happy Coding!
Morten Wennevik [C# MVP]


DaveL said:
Thanks alot...Morten

that is heading in the right direction
but what i understand ..once a dll is loaded in the current domain it can
not be unloaded....
its possible i can have 500 one off .cs files these Represent custom
flexible code for customer needs
each dll would have a lets say static start() method
so the calling application sees the Same Dll and method for each
customer...even though the internal work they will do can and usually
different then other customers

I will work with you example...
I guess i need to find a way to get Assembly info b4 i load it into the
secondary app domain..

Isn't it possible if I have to load it in the current domain to get
assembly info, that i could have a ton of signatures after processing,
since
this will be a service and running 24x7

Currently i use xsl to do the customer specific..but the files can be
pretty
large..
So i would rather use cs Files which give me the power of compiled code
and
flexibility to do any thing .net Can do

Thanks alot
DaveL
Morten Wennevik said:
Hi Dave,

After fidling with some code I think I got a workable solution,
although
it
may not be the most elegant.

The code below assumes there is a Data.dll in a nearby folder, and
loads
it
into a subdomain. The trick is that AppDomain.Load requires assembly
names
or similar instead of the exact file name, so you may need to load it
using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and
obtain
its FullName first. Having the assembly name you can tell the sub
domain
to
look anywhere on the disk for the file, and load it.

protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";

AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);

subDomain.Load(assemblyName);

Assembly[] assemblies = subDomain.GetAssemblies();

Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;

Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);

AppDomain.Unload(subDomain);
}

Once the subdomain is unloaded the assembly file is no longer locked
even
though the CurrentDomain is still running.

--
Happy Coding!
Morten Wennevik [C# MVP]


:

Im looking for some good links
to show me how to load and unload dlls at runtime
specifically

I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain

Thanks
DaveL
 
D

DaveL

Here is my problem...and have been pulling out my hair...lol



// the below code works just fin
//when i create the dll in another folder can't load it i get the Assembly
not found HResult 81?????
//when i create the dll in the CurrentDomain Folder where it loads just
fine..
//how can i get it to load from another folder on the drive
example home folder is d:\bin works
other folder d:\cstest Failes to load


AppDomain newDomain =
AppDomain.CreateDomain("NewDomain",null,AppDomain.CurrentDomain.BaseDirectory,"D:\\cstest",false);
//, null, AppPath, AppPath, false);

try
{

//Assembly asm= newDomain.Load("cstest, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null"); //oName);
Assembly asm = newDomain.Load(@"cstest"); //oName);
}
catch (Exception ex)
{
NetTools.dvErrorSysWin.ShowError(ex);
}


AppDomain.Unload(newDomain);

Morten Wennevik said:
They should all be unloaded when the subdomain unloads. I only used
Assembly.LoadFile in the CurrentDomain to obtain the FullName from my
Data.dll. I'm afraid you can't use this method to obtain the assembly
fullname at runtime, as once the assembly is loaded it will remain so
until
the domain unloads, which basically means the application shutdown if you
load it into the main domain. There may be other ways to obtain the
FullName
though. Besides the FullName is fairly simple if the assembly is not
signed
(publicKey=null).

--
Happy Coding!
Morten Wennevik [C# MVP]


DaveL said:
Thanks alot...Morten

that is heading in the right direction
but what i understand ..once a dll is loaded in the current domain it can
not be unloaded....
its possible i can have 500 one off .cs files these Represent custom
flexible code for customer needs
each dll would have a lets say static start() method
so the calling application sees the Same Dll and method for each
customer...even though the internal work they will do can and usually
different then other customers

I will work with you example...
I guess i need to find a way to get Assembly info b4 i load it into the
secondary app domain..

Isn't it possible if I have to load it in the current domain to get
assembly info, that i could have a ton of signatures after processing,
since
this will be a service and running 24x7

Currently i use xsl to do the customer specific..but the files can be
pretty
large..
So i would rather use cs Files which give me the power of compiled code
and
flexibility to do any thing .net Can do

Thanks alot
DaveL
Morten Wennevik said:
Hi Dave,

After fidling with some code I think I got a workable solution,
although
it
may not be the most elegant.

The code below assumes there is a Data.dll in a nearby folder, and
loads
it
into a subdomain. The trick is that AppDomain.Load requires assembly
names
or similar instead of the exact file name, so you may need to load it
using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and
obtain
its FullName first. Having the assembly name you can tell the sub
domain
to
look anywhere on the disk for the file, and load it.

protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";

AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);

subDomain.Load(assemblyName);

Assembly[] assemblies = subDomain.GetAssemblies();

Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;

Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);

AppDomain.Unload(subDomain);
}

Once the subdomain is unloaded the assembly file is no longer locked
even
though the CurrentDomain is still running.

--
Happy Coding!
Morten Wennevik [C# MVP]


:

Im looking for some good links
to show me how to load and unload dlls at runtime
specifically

I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain

Thanks
DaveL
 

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


Top