Tuesday, March 28, 2006
.NET AppDomain
In ASP.NET web service, use AppDomain to load and unload assembly.
Problem:
Create the assembly with same name, will get error message " access denied, the file is in use by another process"
Solution:
use AppDomain to load and unload the assembly.
Assembly itself can not be unload, so we have to create a new AppDomain and in this new AppDomain to run assembly and finally unload the AppDoamin thus the assembly.
Details:
Good entry point to find anwsers for .NET problem
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=311674&SiteID=1
and the famous paper
Eric Gunnerson has a good article on that sort of thing: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05162002.asp
try
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = m_workDir; //put all .DLL there
setup.ApplicationName = "Mcws";
setup.DynamicBase = m_workDir;
setup.ShadowCopyFiles = "true";
setup.ShadowCopyDirectories = m_workDir;
AppDomain appDomain = AppDomain.CreateDomain(
"Mc", null, setup);
SetAppDomainPolicy(appDomain);
object o= appDomain.CreateInstanceAndUnwrap(
getNamespace(moduleName), //CmmCode.Math ==> CmmCode
moduleName);
MethodInfo mi;
Type t = o.GetType();
mi = t.GetMethod(methodName);
retVal.result = mi.Invoke(o,methodParams);
retVal.success = true;
AppDomain.Unload(appDomain);
appDomain = null;
}