Is there an AppDomain for every C# program even if we do not specifically create an AppDomain? Why is it required? I have read about third party assemblies crashing the entire application if we do not load them into separate AppDomain. I didn't get that point well. Can anyone explain this also.
Every application has at least one application domain, yes.
I don't know what the note about 3rd party assemblies means.
AppDomain
is pretty much like a process, it's an infrastructure that your application runs in. A .NET assembly needs to be loaded in an AppDomain
in order to be run. It's not required to load third party assemblies into separate AppDomains
, but if you do, it provides isolation between them (like two separate processes) and malfunction in one will not affect the other. Application domains can be unloaded independently.
As an example, SQL Server use AppDomain
s to load CLR assemblies safely in its process.
there is at least one appdomain for every program you can create as many as you like but you will rarely need more than one.
Its basically a container where code which is running with a particular trust is running.
There is a default app domain where your application is loaded into (every instance gets its own).
The crash thing means that a third party assembly (i.e. a plugin) will crash your entire application when it crashes if you don't load it into a separate app domain. Therefore it is good practice to load plugins into a separate app domain, because a crash in an app domain will only crash that app domain and not the other domains. The CLR Add-In blog has some posts about this.
An important thing to note is that an app domain does not necessarily need to be in the same process or on the same system, so you basically need it for remoting.
I have read about 3rd party assemblies causing crash if we do not make use of AppDomain
I think you are talking about loading other assemblies in to separate app domains. That way they can be isolated from your address space to prevent a crash in their code affecting you. The cost is that communicating with an assembly in a seperate app domain is more difficult and has a perf penalty as all calls need to be martialed across the app domain boundary.
This is a fairly advance topic, I'd recommend reading up in Richter (other books are avaialable).