473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unload assembly C#

Hi There,

I've got a server that is waiting for requests, these request
correspond to calls to objects that are specified in assemblies stored
in the GAC.

To do this, I use reflection to call the methods.

Now, when I'm trying to optimize the code, I noticed that the
assemblies that are loaded during the reflection call, are never
unloaded.

This is the current problem, the solution: AppDomain.

Using this I load everything in a new AppDomain and then just unload
it, in theory, no problems. When I try to do this, the problems start.

Based on the article written by Eric Gunnerson
(http://msdn.microsoft.com/library/de...p05162002.asp),
I've made something similar, but unfortunaly I got several problems.

Here's my code:
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
object output1;
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "SAP";
AppDomain domain = AppDomain.CreateDomain("SAP",null,setup);
try
{
Assembly assembly1 = domain.Load(assemblyName);

int i=0;
while (domain.GetAssemblies()[i].FullName!=assemblyName)
i++;

Type invokerType1 = domain.GetAssemblies()[i].GetType(className);
object objInvoker1 =
domain.CreateInstanceAndUnwrap(assemblyName,classN ame);
invokerType1.GetProperty("Connection").SetValue(ob jInvoker1,SAPconn,
new object[0]);

output1 = invokerType1.InvokeMember(methodName,BindingFlags. InvokeMethod,null,
objInvoker1,args);

assembly1 = null;
objInvoker1 = null;
invokerType1 =null;

}
finally
{
AppDomain.Unload(domain);
}
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

SAPconn is a object used to connect to a SAP server (not serializable)

When I try to run this I get the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.Serialization.SerializationExceptio n: The type
SAP.Connector.SAPConnection in Assembly SAP.Connector,
Version=1.2.0.0, Culture=neutral, PublicKeyToken=50436dca5c7f7d23 is
not marked as serializable.

The error is thrown at the line:

invokerType1.GetProperty("Connection").SetValue(ob jInvoker1,SAPconn,
new object[0]);

If I use the same code, without using AppDomain (if I don't care that
the assemblies references don't disappear)

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
object output;
Assembly assembly = Assembly.LoadWithPartialName(assemblyName);
Type invokerType = assembly.GetType(className);
object objInvoker = null;
objInvoker = Activator.CreateInstance(invokerType);
invokerType.GetProperty("Connection").SetValue(obj Invoker,SAPconn, new
object[0]);
output = invokerType.InvokeMember(methodName,
BindingFlags.InvokeMethod,
null,
objInvoker,
args);

return output;
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Thanks for all the help,

Luís Pinho
Nov 16 '05 #1
1 11922
Luis:

What you'll want to do is create an assembly that you can have your new
appdomain load up - in this assembly, you have the functionality to use
reflection to invoke a method. Then from your static appdomain, call into
this type, which will actually do the work. This will keep you from having
to serialize 3rd party types between appdomains (which may also require
assembly loading into the default domain)

psuedocode
<In main appdomain>
{
Create AppDomain "domain"
Call "domain.load" loading type "shim"
Call shim's "MyInvokeMethod(typename, methodname)
Unload "domain"
}

<in "shim", which is loaded into new appdomain>
MyInvokeMethod(typename, methodname)
{
Load assembly based on typename
Instantiate new typename
call methodname
return
}
"Luis Pinho" <lp****@oninet.pt> wrote in message
news:bb*************************@posting.google.co m...
Hi There,

I've got a server that is waiting for requests, these request
correspond to calls to objects that are specified in assemblies stored
in the GAC.

To do this, I use reflection to call the methods.

Now, when I'm trying to optimize the code, I noticed that the
assemblies that are loaded during the reflection call, are never
unloaded.

This is the current problem, the solution: AppDomain.

Using this I load everything in a new AppDomain and then just unload
it, in theory, no problems. When I try to do this, the problems start.

Based on the article written by Eric Gunnerson
(http://msdn.microsoft.com/library/de...-us/dncscol/ht
ml/csharp05162002.asp), I've made something similar, but unfortunaly I got several problems.

Here's my code:
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
* object output1;
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "SAP";
AppDomain domain = AppDomain.CreateDomain("SAP",null,setup);
try
{
Assembly assembly1 = domain.Load(assemblyName);

int i=0;
while (domain.GetAssemblies()[i].FullName!=assemblyName)
i++;

Type invokerType1 = domain.GetAssemblies()[i].GetType(className);
object objInvoker1 =
domain.CreateInstanceAndUnwrap(assemblyName,classN ame);
invokerType1.GetProperty("Connection").SetValue(ob jInvoker1,SAPconn,
new object[0]);

output1 = invokerType1.InvokeMember(methodName,BindingFlags. InvokeMethod,null, objInvoker1,args);

assembly1 = null;
objInvoker1 = null;
invokerType1 =null;

}
finally
{
AppDomain.Unload(domain);
}
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*
SAPconn is a object used to connect to a SAP server (not serializable)

When I try to run this I get the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.Serialization.SerializationExceptio n: The type
SAP.Connector.SAPConnection in Assembly SAP.Connector,
Version=1.2.0.0, Culture=neutral, PublicKeyToken=50436dca5c7f7d23 is
not marked as serializable.

The error is thrown at the line:

invokerType1.GetProperty("Connection").SetValue(ob jInvoker1,SAPconn,
new object[0]);

If I use the same code, without using AppDomain (if I don't care that
the assemblies references don't disappear)

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
* object output;
Assembly assembly = Assembly.LoadWithPartialName(assemblyName);
Type invokerType = assembly.GetType(className);
object objInvoker = null;
objInvoker = Activator.CreateInstance(invokerType);
invokerType.GetProperty("Connection").SetValue(obj Invoker,SAPconn, new
object[0]);
output = invokerType.InvokeMember(methodName,
BindingFlags.InvokeMethod,
null,
objInvoker,
args);

return output;
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*
Thanks for all the help,

Luís Pinho

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lloyd Sheen | last post by:
I am having trouble gettting an assembly loaded with Assembly.LoadFrom. I can get the assembly call the method I need to call. Now I want to unload the assembly. I need to do this so that the...
0
by: Lloyd Sheen | last post by:
I am having trouble gettting an assembly loaded with Assembly.LoadFrom. I can get the assembly call the method I need to call. Now I want to unload the assembly. I need to do this so that the...
1
by: Andy | last post by:
Hi everybody, i´m working on an application that loads different modules (dll files) via an assembly. I check the build version in that dll file and if a newer version exists, i want to update...
4
by: Mirano | last post by:
Hi everybody. I load an assembly into another AppDomain, not a default one. As there is no way to unload the assembly, I need to unload the domain. This is where the app hangs. The problem is...
2
by: Lauren Hines | last post by:
Hello, I have read numerous post stating that the only way to unload an assembly (DLL in my case) is to create a separate AppDomain, load the assembly, then unload it by calling AppDomain.Unload....
2
by: Sam Martin | last post by:
Morning all, Right, I've read untold articles now, listening to people bitch about there being no Unload method for Assembly. Plenty of people do counter that this is possible by loading the...
6
by: Wal Turner | last post by:
Hi there. There are various snippets on forums regarding issues with AppDomain.Unload and how it just doesnt work. Fortunately, I got it working with the base case, after much fiddling. Consider...
2
by: brianbender | last post by:
I am trying to load and unload assemblies dynamically and call methods and properties when loaded into an Appdomain I can load assemblies all day in the current AppDomain without references and...
2
by: Dominique Vandensteen | last post by:
I want to make a program that checks for updates at start... The way I am doing this is to create 2 projects... A simple exe checks the server for the version and downloads a dll if needed. The...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.