Problem: After unloading the AppDomain it is not allowing to rebuild the DLL

  • Thread starter Thread starter Raveendra M
  • Start date Start date
R

Raveendra M

Hi!
I am working with ASP.NET application. In my page I am
creating one Application Domain and in that domain I am
calling my DLL. Using the methods of the dll. And
unloading the Application Domain. Till this point it is
working fine.
My problem is, while web page is open, than I am modifying
the dll, and rebuilding. Then it is giving error:
"The file 'MyClass.dll' cannot be copied to the run
directory. The process cannot access the file because it
is being used by another process."

How do I over come this problem?

My ASPX page code is :
==========================================================
private void Page_Load(object sender, System.EventArgs e)
{
InitMethod();
}
private void InitMethod()
{
AppDomainSetup info = new
AppDomainSetup();
info.ApplicationBase = "file:///"
+ System.Environment.CurrentDirectory;
// Create an application domain
with null evidence
AppDomain dom =
AppDomain.CreateDomain("RemoteDomain", null, info);
// Tell the AppDomain to execute
the assembly
System.Reflection.Assembly a =
System.Reflection.Assembly.LoadFrom
("D:\\Raveendra\\APPDomain\\MyClass\\bin\\Debug\\MyClass.dl
l");
object o = a.CreateInstance
("MyClass.Class1",
false,

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod,
null,
null,

null,
null);

System.Type t = o.GetType ();

Object result = t.InvokeMember
( "FindSum",

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod ,
null, o, new object [] {});

Response.Write (result);
//Call a method with arguments
object [] args = new object []
{100, 184};

Object result1 = t.InvokeMember
( "FindSum1",

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod ,
null, o, args);
TextBox1.Text = result1.ToString
() ;
AppDomain.Unload(dom);
}

==========================================================
In brief my problem is :
when the try to build the Dll in the
path "D:\\Raveendra\\APPDomain\\MyClass\\bin\\Debug\\MyClas
s.dll", while ASP.NET page is running, it is not allowing
me. How to over come this problem, with out stoping the
IIS?

Thanks for your help.
 
When you call

System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom()

it causes the assembly to be loaded in the same appdomain that the code is
executing in (the default appdomain), not from the new appdomain
("RemoteDomain") that you just created - once loaded in the default
appdomain you cannot unload it. You need to create a helper class derived
from MarshalByRefObj, create an instance of the object in the "RemoteDomain"
that you use via remoting, and call a method in that object that will load
the assembly you want to use.

This link explains what you need to do.
http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx



Raveendra M said:
Hi!
I am working with ASP.NET application. In my page I am
creating one Application Domain and in that domain I am
calling my DLL. Using the methods of the dll. And
unloading the Application Domain. Till this point it is
working fine.
My problem is, while web page is open, than I am modifying
the dll, and rebuilding. Then it is giving error:
"The file 'MyClass.dll' cannot be copied to the run
directory. The process cannot access the file because it
is being used by another process."

How do I over come this problem?

My ASPX page code is :
==========================================================
private void Page_Load(object sender, System.EventArgs e)
{
InitMethod();
}
private void InitMethod()
{
AppDomainSetup info = new
AppDomainSetup();
info.ApplicationBase = "file:///"
+ System.Environment.CurrentDirectory;
// Create an application domain
with null evidence
AppDomain dom =
AppDomain.CreateDomain("RemoteDomain", null, info);
// Tell the AppDomain to execute
the assembly
System.Reflection.Assembly a =
System.Reflection.Assembly.LoadFrom
("D:\\Raveendra\\APPDomain\\MyClass\\bin\\Debug\\MyClass.dl
l");
object o = a.CreateInstance
("MyClass.Class1",
false,

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod,
null,
null,

null,
null);

System.Type t = o.GetType ();

Object result = t.InvokeMember
( "FindSum",

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod ,
null, o, new object [] {});

Response.Write (result);
//Call a method with arguments
object [] args = new object []
{100, 184};

Object result1 = t.InvokeMember
( "FindSum1",

System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.InvokeMethod ,
null, o, args);
TextBox1.Text = result1.ToString
() ;
AppDomain.Unload(dom);
}

==========================================================
In brief my problem is :
when the try to build the Dll in the
path "D:\\Raveendra\\APPDomain\\MyClass\\bin\\Debug\\MyClas
s.dll", while ASP.NET page is running, it is not allowing
me. How to over come this problem, with out stoping the
IIS?

Thanks for your help.
 
Back
Top