HOSTING:
ASSEMBLIES:
REFLECTION:
- MSCorEE.dll - SHIM - has CorBindToRuntimeEx function which is responsible for loading CLR Com Server.
- Therer will be only one SHIM per m/c which is installed by latest ver of CLR.(in 64 bit win there will be 2 shim)
- Latest shim knows how to find any previous version of CLR.
- MSCorWks.dll - CLR Com Server - which is loaded by mscoree.dll based on the input parameters.
- By default shim examines the executable header to get the version of clr required to execute it but it can be
overwritten by placing RequiredRunTime and SupportedRunTime in the XML file.this is passed in CorBindToRuntimeEx - One windows process can load only one version of CLR.Once loaded CLR can't be unloaded it goes off when process terminates.
- Appdomain is a logical container for a set of assemblies.
- First Appdomain is loaded when CLR is initialized - default appdomain - only unloaded when win process terminates.
- Common appdomain is for domain neutral - which also can't be unloaded.common assemblies like MSCorLib.dll.
- Other appdomain can be unloaded. Single assembly can't be unloaded from an appdomain.
- Appdomain didn't share memory and they have their own loader heap - records which type has been accessed since appdomain created.
- CREATEDOMAIN - create new appdomain at runtime - input - name,Evidence object,AddomainSetup object -for config
- CreateInstanceAndUnwrap - appdomain method will load assembly into appdomain- input assembly name and type name.
- If required type is drevied from MarshalByRefObject than this mtd marshal the object by reference.By reference means sending proxy.
- Object dervied from MarshalByRefObject didn't have instance field CLR uses reflection to get and set instance field.
- Proxy object instance field are used to locate the type in target assebly.Proxy is not of actual type instead it isMarshalByRefObject type.
- For creating type in other appdomain current thread transit to new appdoamin.
- Method call from one appdomain to other will cause thread transition i.e. execution is synchronus --for asynchronus call u need to create new threads.
- When target domain unloaded proxy didn't get update so any further call will give AppdomianUnloadException.
- Object derived from MarshalByRefObject should avoid any static member.As static run in context of current appdomain no proxy object exist in such case.
- Security context is same in thread transition unless new domain is created with different evidence and config setting.
- To maintian isolation Reference can't be directly passed b/w appdoamins. either proxy or byValue.
- IsTransparentProxy on instance can tell weather it is proxy or real object.
- If CreateInstanceAndUnwrap find that type is not derived from MarshalByRefObject then it can't pass proxy so it check if -- type has [serializable] attribute if yes it can be passed as byValue else it will throw SerializationException.
- In marshallig object by value calling domain security and config settings will be applied.No thread transition
so type can be used accross appdomain only if it is derived from MarshalByRefObject or marked as [serializeable]. - No references can be passed accross appDoamin to maintain isolation.- Basic Rule
- String type are handled specially their reference are passed across as it is immutable and not possible to get corrupted by other appdomain.
- unloading also done by same thread but if can't unload in 10 sec throw cannotunloadappdomainexception
- Windows app host - Window process loads shim which furter loads CLR which in turn load requred assemblies in appdomain.When main method returns, the window process terminates(unloading default and other appdomains)
System.Environment's Static EXIT method terminates window process gracefully,It first called finalize of all object --releass all of the unmanaged COM object held by CLR and unload all appdomains. - IE as a host - MIME fileter(MSCorIE.dll) is hooked in ie 5.01 and later.
- ASP.net host CLR and CLR load Appdomain one for each web site.
- SHADOW COPYING (appdomain feature) - whan asp.net files changes on hardisk it unload appdomain and reload new one with changed files
- All assemblies in GAC are always granted full-trust.
- Loadin assemblies in domain neutral area can increase the performance as it's reduce the momore space and also it's compiled once for all appdomains but -ves are it can't be unloaded and also their will be marshalling cost + if any assembly is loaded in domain neutral are has some dependent assemblies then those assemblies also need to be loaded and there are chances of crosss referencing so only simple independent dlls are prefered as domain neutral liek msCorlib.
- aspnet_isapi - ISAPI module - Internet service application programming Interface. process incoing request and delegate it to proper handler.
- aspnet_wp.exe - worker process runs under default aspnet account, which get created when aspnet is installer.
- IIS 5.0 process Model - aspnet_isapi receives the request and transfer it to aspnet_wp.exe thru name pipes (communication cost is high) now worker process loads the CLR and start ASP.net runtime pipeline to process the request. Each application is loaded in different appdomain but they all share the same worker process.
- IIS 6.0 process Model: Application pool is introduced which is the logical grouping of one or more application. Each application pool has it's own worker process.Each app pool and it's copy of worker process can be configured with different set of properties.
- In IIS 6.0 All http request in intercep by a kernel mode device driver named http.sys, it routes the request to application pool that the invoked application belogns to. Now the worker process (w3p.exe) of the pool loads aspnet_isapi.dll which in trun loads CLR and start the asp.net runtime pipe line to process the request. Since aspnet_isapi.dll itself is loading the CLR there is no cost of communication b/w them as it is in IIS 5.0
- Worker process runs under Network Service account and built in asp.net worker process is disabled.
ASSEMBLIES:
- Assembly Version - Major:Minor:Build:Revision
- Assembliey with cluture is called - Satellite Assembly
- Idealy SA should not have any code so no need to build it.SA Should reside in Sub dir with name as culture (en-US)
- Week assemblies did't have public key and always deployed privately. Searched with following algo:
- Probing
1. CLR will probe only if referenced Assembly as mentioned in AssemblyRef meta is not found.
2. if you remove some assembly from bin then filenotfoundexception will be thrown at runtime with it tries to access it
3. but if you used assemblybinding config setting CLR can probe to find it at some other location.
4. Base - AsmName.dll >> AsmName\AsmName.dll this is searched in:
5. For Cuilture Neutral - Appdir >> private path specified in probing element in config file. >> same seq for .exe.
* For culture specific- Appdir/en-US/ >> private path\en-US >> .exe >> only with culture en for dll then exe. - Strongly Named:
1. file name + culture + version + Public key token. - GACUtil.exe - -i to install assem into gac,-u to uninstall -r - to bind assembly with particular assembly (use either i or r)
- Delayed signing or Partial Signing -p - sign assembly only with company's public key - during development pahse. In delayed signing files are not hashed and digital signature is not embeded in assembly.
- SN.exe - generates a file which contains public and private key which can assessed using filename.publickey or filname.privatekey
- Only administrator can install assembliies into GAC.
- SNA (strong name assembly) can be deployed in any directory - codeBase config setting will identify this directory.
- codeBase - can be disck locaiton or web address.in case of web address assembly is loaded into cache and time stamp is check to reload if modified.
- if in GAC verification(no tampering by manifest) will be done only once while installing for other dir every assemly is loaded verificatin will be done.
- SNA first searched in GAC and than in AppBase.
- UNIFICATION - FCL assem tied to the version of CLR running.Version MSCorLib.dll loaded will match their version of CLR inspit of any other version app is refering.
- SNA >> If not in GAC and no codeBase is givein in config >> Probe as in WNA.
- if found in GAC then apply version redirect.
- For version redirect check first what redirection is specified in web.config file. then if publisher policy is applied (or no node) then apply publisher redirect as specifid in publisher policy assembly in GAC.
- then check for machine.config and apply any redirection given in m/c config file.
- codeBase can be used for week assem also but in that case it hold subdir inside appdir not URL.
- withoug version redirect we need to complie our applicatin again in case we want to use new assembly.
- Publisher policy is applied only on major and minor version.Not of build andd revision version.
- At run time CLR loads assembly using System.Reflection.Assembly's Static Load method.
- Appdomain has it's own setting to load any assembly into it, like it can specify probing privatepath to locate any assembly to load.
- Appdomain.load - instance method;designed for unmanaged code.loads the assembly into specified appdomain based on it's setup
- Assembly.load is static method and load assembly into current domain based on current appdomain setup.
- When we say Assembly as = ad.load - what it does is:
- 1. load assembly into ad based on ad settings.
- 2. Assembly is not derived from MarshalbyRefObject so it tries to copy assembly in current appdomain
- 3.Now this in turn mean serialization system of current appdomain tries to load it based on it's settings. Based on the setting probing may not point to the path from where you are trying to laod the assembly thus file not found ex is thrown - FIleNotFoundEx.
- So either add assembly path to privatepath of calling assem or paste calling assembly in appdir or in privatepath of current appdomain.
- Assembly.LoadFrom(dllLocation) >> first it call GetName and then Load passing assembly ref from getname
- At Assembly.Load CLR applies version-binding and if it fails, LoadFrom load the assembly from specified location.
- Assembly.LoadFile - load assembly without from specified path without any policies applied by CLR.
- Assembly.ReflectionOnlyLoad / ReflectionOnlyLoadFrom - load assembly only for metadata analysis.No code can be executed;ReflectionOnlyLoadFrom - will not search anywhere else other than specified path,ReflectionOnlyLoad - does search on privatepath,codebase but didn't apply version policy;When using ReflectionOnly methods we need to register ReflectionOnlyAssemblyResolve event for callback which loads ,refereced assemblies.CLR doesn't load referenced assemblies automatically.
REFLECTION:
- Reflection is generatlly used by classlibraries to undersntad type definition in order to provide some rich functionality like:
- Serialization uses reflection to find what field a type define and then formatter get their values & put them in stream.
- Or Reflection is used when to load a specific type from specific assembly to accomplish some task at run time - late binding
- GetType - Return the type of the object at run time - late bindign.
- TypeOf - returns the type of the specified class early bound- typof is operator and produces faster code then GetType.
- Starting point of reflection is Type.Once you have type of object you can do anything.
- For Creating Instance of Type following class provide different methods-
- Activator, Appdomain,Type, Reflection, ConstructorInfo
- Return type of createMethods either return reference of New object or ObjectHandle; System.Runtime.Remoting.Objecthandle derived from MarshalByRefObject
- ObjectHandle - Used to pass object(just refernce) from one Appdomain to other.When object is actually required, ObjectHandle.Unwrap, need to call to either create proxy(for ByRef) or derialize Object (for ByValue).
- Activator - CreateInstance - (obj refr or objectHandle),CreateInstanceFrom (use assembly.loadfrom) objectHandle.
- Appdomain - CreateInstance,CreateInstanceFrom,CreateInstanceAndUnwrap,CreateInstanceFromAndUnwrap - similar to activaor's mtd.
- System.Type - InvokeMember - crate object in calling appdomain and return reference.
- Reflection.ConstructorInfo - Invoke same as system.type.
- All these mtd call constructor to create object.Activator.CreateInstance allow to create instance of value type without calling constructor.
- Objcets of all types can be created by these methods except for Arrays(array.CreateInstance) and Delegates(Delegate.CreateDelegate)
- Discovering Members thru REflection - Appdomain >> Assemblies >> Modules or Types >> Types >> other types like (Method,Constr,Events,Prop etc)
- Appdomain.GetAssemblies >> Assembly.GetExportedTypes() -t >> t.GetMembers -memberInfo >> types (NAme spaces and Interface to be retrived seperately).
- BindFlags is used to filter the types requred to analyses in GetMembers method.
- System.Type's - InvokeMember is used to invoke member of created type.
- Invoking behavior depend on the kind of member invoked - for fieldInfo invoking means getting and setting fields; invoking.Constructorinfo - let u create instance on invoking.MethodInfo - let u call method.
- Invoking is two step process - first it uniquely binds the member to be Invoked and then Invoke it.
- Although private members can be accessed by reflection but it uses CAS to ensure it's power isn't abused.