Que: What is AppDomain?
Ans: AppDomain is logical container for set of assemblies.It is kind of shell to make appliction secure. It is different from a process. A process can have multiple AppDomains. It is kind of isolation of memory used by application. As the memory in appdomain is managed by CLR, so it is responsibility of CLR to make sure that one appdomain doesn't access the memory of other appdomain.
Que: What is difference between AppDomain and a process?
Ans: A process is OS level concept whereas AppDomain is .NET level concept. The purpose of both is to provide security but at different level. An AppDomain can belong to only one process, but a process can have more than one AppDomain in it.
Que: What is the advantage of having AppDomain?
Ans: We are having concept of process to provide isolation of memory, but still .NET have given AppDomains, because creating a process is always costly. AppDomain creation is .NET level so creating an AppDomain is not so costly. Moreover unloading an AppDomain causes unloading all the assemblies loaded inside that AppDomain.and unloading one appdomain never affects another appdomain.
Que. If AppDomain is isolating the memory usage, than is it possible to access object in one AppDomain in another AppDomain?
Ans:It is possible that code in one appdomain can communicate with types and objects in another appdomain.
Object by reference:
If an object have to get shared by reference, that class has to inherit from "MarshalByRefObject" class.
Actually when an object is passed as reference from one appdomain to another, then CLR create a proxy object at destination. This proxy looks exactly same as type of object we want pass, but it has information about how to access that object in another appdomain(where original object got created).So the actually one appdomain still not accessing the memory of another appdomain. but CLR manages this by itself. Now when we try to use that object(by executing some method in it) through proxy, actually Appdomain transition occures and we reaches to original object(and executes the method).
Object by Value:
if we want to pass an object by value from on appdomain to another we need to add [Serializable] attribute to that class. So ih this case all field values of original object gets copied to destination appdomain. So if we unload the appdomain still the object exist in destination appdomain. Actually CLR creates exact replica of original object in destination.
Que: How to unload an appdomain?
Ans:Appdomain has a static unload method which will unload the appdomain.All threads running in appdomain and also those thread which can return at some point , get unloaded. CLR forces all the threads involve with unloading appdomain to throw ThreadAbortException. During shutdown no new threads are allowed to enter the application domain and all application domain specific data structures are freed. You cannot unload an application domain in a finalizer or destructor. If the application domain has run code from a domain-neutral assembly, the domains copy of the statics and related CLR data structures are freed, but the code for the domain-neutral assembly remains until the process is shutdown. There is no mechanism to fully unload a domain-neutral assembly other than shutting down the process.
Note:
Application domain is not a secure boundary when the application runs with full trust. Applications running with full trust can execute native code and circumvent all security checks by the .NET runtime. ASP.NET applications run with full trust by default.
Ans: AppDomain is logical container for set of assemblies.It is kind of shell to make appliction secure. It is different from a process. A process can have multiple AppDomains. It is kind of isolation of memory used by application. As the memory in appdomain is managed by CLR, so it is responsibility of CLR to make sure that one appdomain doesn't access the memory of other appdomain.
Que: What is difference between AppDomain and a process?
Ans: A process is OS level concept whereas AppDomain is .NET level concept. The purpose of both is to provide security but at different level. An AppDomain can belong to only one process, but a process can have more than one AppDomain in it.
Que: What is the advantage of having AppDomain?
Ans: We are having concept of process to provide isolation of memory, but still .NET have given AppDomains, because creating a process is always costly. AppDomain creation is .NET level so creating an AppDomain is not so costly. Moreover unloading an AppDomain causes unloading all the assemblies loaded inside that AppDomain.and unloading one appdomain never affects another appdomain.
Que. If AppDomain is isolating the memory usage, than is it possible to access object in one AppDomain in another AppDomain?
Ans:It is possible that code in one appdomain can communicate with types and objects in another appdomain.
Object by reference:
If an object have to get shared by reference, that class has to inherit from "MarshalByRefObject" class.
Actually when an object is passed as reference from one appdomain to another, then CLR create a proxy object at destination. This proxy looks exactly same as type of object we want pass, but it has information about how to access that object in another appdomain(where original object got created).So the actually one appdomain still not accessing the memory of another appdomain. but CLR manages this by itself. Now when we try to use that object(by executing some method in it) through proxy, actually Appdomain transition occures and we reaches to original object(and executes the method).
Object by Value:
if we want to pass an object by value from on appdomain to another we need to add [Serializable] attribute to that class. So ih this case all field values of original object gets copied to destination appdomain. So if we unload the appdomain still the object exist in destination appdomain. Actually CLR creates exact replica of original object in destination.
Que: How to unload an appdomain?
Ans:Appdomain has a static unload method which will unload the appdomain.All threads running in appdomain and also those thread which can return at some point , get unloaded. CLR forces all the threads involve with unloading appdomain to throw ThreadAbortException. During shutdown no new threads are allowed to enter the application domain and all application domain specific data structures are freed. You cannot unload an application domain in a finalizer or destructor. If the application domain has run code from a domain-neutral assembly, the domains copy of the statics and related CLR data structures are freed, but the code for the domain-neutral assembly remains until the process is shutdown. There is no mechanism to fully unload a domain-neutral assembly other than shutting down the process.
Note:
Application domain is not a secure boundary when the application runs with full trust. Applications running with full trust can execute native code and circumvent all security checks by the .NET runtime. ASP.NET applications run with full trust by default.