Wednesday 30 May 2007

Questions related with assemblies

Q .How should an assembly be uninstalled
A.Uninstalling an assembly should be done with care.
The following command removes the assembly hello from the global assembly cache(GAC) as long as no reference counts exist for the assembly.
gacutil /u hello

Note : If there is only one version of hello assembly the above command is fine and you are in safer side. Incase if there are more than one version of the assembly or different assembly with same name exists then the above command might remove more than one assembly from the assembly cache because the assembly name is not fully specified. For example, if both version and of hello are installed in the cache, the command gacutil /u hello removes both of the assemblies.

Then how to remove the assembly safely: Use the following example to avoid removing more than one assembly. This command removes only the hello assembly that matches the fully specified version number, culture, and public key.
gacutil /u hello, Version=, Culture="de", PublicKeyToken=45e343aae32233ca

Q . How do I see the physical file structure of GAC in explorer and how can one debug assemblies in GAC?

A. It is possible to see physical file structure of GAC folder.

As such Winnt\assembly\gac cannot be browsed using windows explorer.
To view the physical file structure add a binary value named 'DisableCacheViewer' to the registry key HKLM\Software\Microsoft\Fusion and set it to a non-zero value.

An alternative to this is by opening visual studio command prompt and navigating to
cd c:\%windir%\assembly\gac and listdown the directories by using the dir commandthere will be individual directory for every assembly installed in GAC.If you will further drilldown
to the hierarchy of assembly you will see a folder starting with version number and ending with public key of the assembly
something like : 1.0.5000.0__b03f5f7f11d50a3a
In thats particular folder you will find the copy of the assembly , if you want to debug an assembly which is installed in gac(Shared Assembly) then you need to copy the pdb file here.

Thursday 17 May 2007

Q.What is an Assembly
A. Assembly is a reusable, versionable, and self-describing building block of a common language runtime application.

Assemblies provide the infrastructure that allows the runtime to fully understand the contents of an application and to enforce the versioning and dependency rules defined by the application. These concepts are crucial for solving the versioning problem and for simplifying the deployment of runtime applications.

Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.

In general, a static assembly can consist of four elements:

.The assembly manifest, which contains assembly metadata.
.Type metadata.
.Microsoft intermediate language (MSIL) code that implements the types.
.A set of resources.

Q Const and ReadOnly whats the difference ?

A. Hi :-)
Ever wondered whats the difference between ReadOnly and Const fields in .Net Type. Well i got the answer yesterday...( i suggest Applied .Net Framework Programming by Jeffery Richter as reference).

The values of all the Const fields should be known to the C# compiler at compile time. This value is then embedded in the metadata of the assembly that contains the type which has Const members.

using System;
namespace spider
class useless
public const int maxVotes=30;


when the above is compiled the IL generated had 30 instead of maxVotes.
This can cause a versioning problem when we are talking about using constants defined in a different assembly.
lets take an example
imagine that another assembly were to use the const defined in the spider namespace ..(lets say the spider namespace is compiled into spider.dll).

using System;
using Spider;
namespace octopus
class TestConst
Console.WriteLine("The value of the constant : {0}",spider.useless.maxVotes);

lets say we compile the above code to get the octopus.exe.

Now lets assume that we change spider.dll to make the value of maxVotes=100,
so now when we run the octopus assembly we expect the value 100 to printed but that does not happen , the program still outputs the value 30. This is because the constant spider.useless.maxVotes is evaluated at compile time of the octopus.exe so unless we re compile octopus.exe we would not see the change reflected.

If U want to do away with this problem then the read only fields are a solution. They are evaluated at runtime.

Q. What is Finalize?

A. Finalize method acts as a safeguard to clean up resources in the event that your Dispose method is not called.

This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory

Finalize operations have the following limitations:

1.The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.

2.The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.
The thread on which the finalizer is run is unspecified.

The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:

Another finalizer blocks indefinitely (goes into an infinite loop, tries to obtain a lock it can never obtain and so on). Because the runtime attempts to run finalizers to completion, other finalizers might not be called if a finalizer blocks indefinitely.
The process terminates without giving the runtime a chance to clean up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification.

A type must implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed. See the IDisposable interface for a complementary and more controllable means of disposing resources.

Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily. Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections. When the garbage collector performs a collection, it reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list. A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.

Note:Finalize is protected and, therefore, is accessible only through this class or a derived class.

Q Difference between Build and Rebuild options in .NET
A. VisualStudio.Net provides us with two options for compiling and creating builds for our application. They are Build Solution and Rebuild Solution options which can be accessed from the Build menu. The differences between these two options are
1. The Build Solution option compiles only those project files and components that have changed since the last build. For example consider that you have two projects Proj1 and Proj2 in your solution MySolution. When you compile the solution using Build Solution option after making some changes to Proj1 only Proj1 will be compiled and built but Proj2 will not be compiled since there are no changes to it.
2. On the other hand the Rebuild Solution option builds all project files and components irrespective of the changes made to them. For example consider that you have two projects Proj1 and Proj2 in your solution MySolution. When you compile the solution using Rebuild Solution option after making some changes to Proj1, both Proj1 and Proj2 will be compiled and built even though there are no changes made to Proj2.

Q. What Is XSD

A The XML Schema definition language (XSD) enables you to define the structure and data types for XML documents