Monday, May 21, 2007

.NET Assemblies and Components

What is an Assembly?
An assembly is a file that is automatically generated by the compiler upon successful compilation of every .NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated. The entire process will run in the background of your application; there is no need for you to learn deeply about assemblies. However, a basic knowledge about this topic will help you to understand the architecture behind a .NET application.

Assembly:
When C# program is compiled, it is packaged into an assembly. An assembly is a .NET executable program (or part of an executable program delivered as a single unit.
It is a file or set of files containing a .NET program or resources supporting a program. When you build a C# windows or console application, the .exe file produced is an assembly. If you build a class library the DLL (Dynamic Link Library) file produced is also an assembly.
All the code in an assembly is built, delivered, and assigned a version number as a single unit. The assembly makes the public classes, properties, and methods visible to other programs. Everything private to your program is kept inside the assembly.

Components
A component is a subprogram or part of a program designed to be used by other programs. In addition, a component is a binary unit that can be used by other programs without having to recompile either the source code of the component itself or the program using the component. This means that 3rd party doesn’t have to provide the source code for their components.
· A component includes any binary subprograms, thus any DLL is by definition a component, since it is a subprogram containing executable code.
· A component requires to provide a means of advertising its contents to other programs. Assemblies provide this advertising ability within .NET.
· A component in the .NET Framework must implement the System.ComponentModel.IComponent interface, which includes methods that can be called by other components to release no-longer-used system resources and to support integration with design tools

Benefits of Components
· Components provide
· Improved reusability
· Flexibility and
· Delivery of subprograms
· Binary reuses saves time and increase reliability
If could share the components (written by others) at the binary level, you wouldn’t have to worry about what programming languages was used to develop the component.

DLL (Dynamic Link Library)
MS introduced the DLL where one or more programs could use a chunk of code stored in a separate file. This worked at a very basic level if the programs were written in the same language (typically C). However, programs needed to know a lot in advance about the DLLs they used, and DLLs did not enable programs to use one another’s data.

DDE (Dynamic Data Exchange)
To exchange data, DDE was developed. This defined format and mechanism for piping data from one program to another, but was not flexible

OLE 1.0 (Object Linking and Embedding)
OLE 1.0 enabled a document such as word to actually contain a document from another program (such as Excel). This was something like components, but OLE 1.0 was not truly a general-purpose component standard.

COM (Component Object Model)
MS implemented COM in windows in the mid-1990s. OLE version 2 and many successor technologies were built on COM.

DCOM (Distributed COM) introduced the ability for COM Components to interact over a network.

COM+ added services that components could call on to ensure high performance in multitier environments
COM works well but is difficult to learn and use, especially when used from C++. COM requires information about components to be inserted into the Windows System Registry, making installation more complex and component removal more difficult.

What is Assembly?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.Assemblies are a fundamental part of programming with the .NET Framework. An assembly performs the following functions:
· It contains code that the common language runtime executes. Microsoft intermediate language (MSIL) code in a portable executable (PE) file will not be executed if it does not have an associated assembly manifest. Note that each assembly can have only one entry point (that is, DllMain, WinMain, or Main).
· It forms a security boundary. An assembly is the unit at which permissions are requested and granted.
· It forms a type boundary. Every type's identity includes the name of the assembly in which it resides. A type called MyType loaded in the scope of one assembly is not the same as a type called MyType loaded in the scope of another assembly.
· It forms a reference scope boundary. The assembly's manifest contains assembly metadata that is used for resolving types and satisfying resource requests. It specifies the types and resources that are exposed outside the assembly. The manifest also enumerates other assemblies on which it depends.
· It forms a version boundary. The assembly is the smallest versionable unit in the common language runtime; all types and resources in the same assembly are versioned as a unit. The assembly's manifest describes the version dependencies you specify for any dependent assemblies.
· It forms a deployment unit. When an application starts, only the assemblies that the application initially calls must be present. Other assemblies, such as localization resources or assemblies containing utility classes, can be retrieved on demand. This allows applications to be kept simple and thin when first downloaded.
· It is the unit at which side-by-side execution is supported.
· Assemblies can be static or dynamic. Static assemblies can include .NET Framework types (interfaces and classes), as well as resources for the assembly (bitmaps, JPEG files, resource files, and so on). Static assemblies are stored on disk in PE files. You can also use the .NET Framework to create dynamic assemblies, which are run directly from memory and are not saved to disk before execution. You can save dynamic assemblies to disk after they have executed.There are several ways to create assemblies. You can use development tools, such as Visual Studio .NET, that you have used in the past to create .dll or .exe files. You can use tools provided in the .NET Framework SDK to create assemblies with modules created in other development environments. You can also use common language runtime APIs, such as Reflection.Emit, to create dynamic assemblies.

What are the contents of 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.

What are the different types of assemblies?Private, Public/Shared, Satellite

What is the difference between a private assembly and a shared assembly?
· Location and visibility: A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory beneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful, e.g. the .NET framework classes.
· Versioning: The runtime enforces versioning constraints only on shared assemblies, not on private assemblies.

What are Satellite Assemblies? How you will create this? How will you get the different language strings? Satellite assemblies are often used to deploy language-specific resources for an application. These language-specific assemblies work in side-by-side execution because the application has a separate product ID for each language and installs satellite assemblies in a language-specific subdirectory for each language. When uninstalling, the application removes only the satellite assemblies associated with a given language and .NET Framework version. No core .NET Framework files are removed unless the last language for that .NET Framework version is being removed.(For example, English and Japanese editions of the .NET Framework version 1.1 share the same core files. The Japanese .NET Framework version 1.1 adds satellite assemblies with localized resources in a \ja subdirectory. An application that supports the .NET Framework version 1.1, regardless of its language, always uses the same core runtime files.)http://www.ondotnet.com/lpt/a/2637 **

How will u load dynamic assembly? How will create assemblies at run time?**

What is Assembly manifest? what all details the assembly manifest will contain?Every assembly, whether static or dynamic, contains a collection of data that describes how the elements in the assembly relate to each other. The assembly manifest contains this assembly metadata. An assembly manifest contains all the metadata needed to specify the assembly's version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information.It contains Assembly name, Version number, Culture, Strong name information, List of all files in the assembly, Type reference information, Information on referenced assemblies.

Difference between assembly manifest & metadata?assembly manifest - An integral part of every assembly that renders the assembly self-describing. The assembly manifest contains the assembly's metadata. The manifest establishes the assembly identity, specifies the files that make up the assembly implementation, specifies the types and resources that make up the assembly, itemizes the compile-time dependencies on other assemblies, and specifies the set of permissions required for the assembly to run properly. This information is used at run time to resolve references, enforce version binding policy, and validate the integrity of loaded assemblies. The self-describing nature of assemblies also helps makes zero-impact install and XCOPY deployment feasible.metadata - Information that describes every element managed by the common language runtime: an assembly, loadable file, type, method, and so on. This can include information required for debugging and garbage collection, as well as security attributes, marshaling data, extended class and member definitions, version binding, and other information required by the runtime.

What is Global Assembly Cache (GAC) and what is the purpose of it? (How to make an assembly to public? Steps) How more than one version of an assembly can keep in same place?Each computer where the common language runtime is installed has a machine-wide code cache called the global assembly cache. The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer. You should share assemblies by installing them into the global assembly cache only when you need to.Steps
- Create a strong name using sn.exe tooleg: sn -k keyPair.snk- with in AssemblyInfo.cs add the generated file name eg: [assembly: AssemblyKeyFile("abc.snk")]- recompile project, then install it to GAC by eitherdrag & drop it to assembly folder (C:\WINDOWS\assembly OR C:\WINNT\assembly) (shfusion.dll tool)orgacutil -i abc.dll

If I have more than one version of one assemblies, then how'll I use old version (how/where to specify version number?) in my application?**

How to find methods of a assembly file (not using ILDASM)Reflection

Assemblies
An assembly is the logical unit that contains compiled code targeted at the .NET Framework. An assembly is completely self-describing, and is logical rather than a physical unit, which means that it can be stored across more than one field. If an assembly is stored in more than one file, there will be one main file that contains the entry point and describes the other files in the assembly.
The assembly structure is used for both executable code and library code. The only difference is that an executable assembly contains a main program entry point, whereas a library assembly doesn’t.
An important characteristic of assemblies is that they contain metadata that describes the types and methods defined in the corresponding code. Also it contains assembly metadata that describes the assembly itself.

Assemblies come in two types:
1. Shared Assemblies
2. Private Assemblies

Private Assemblies: are the simplest types. They normally ship with software and are intended to be used only with that software. The usual scenario in which you will ship private assemblies is when you are supplying an application in the form of an executable and a number of libraries, where the libraries contain code that should only be used with that application.

Shared Assemblies: are intended to be common libraries that any other application can use. Because any other s/w can access a shared assembly, more precautions need to be taken against the following risks.
· Name Collisions, where another company’s shared assembly implements types that have the same names as those in your shared assembly.
· The risk of assembly being overwritten by a different version of the same assembly.
The solution to these problems involves placing shared assemblies in a special directory subtree in the file system, known as the global assembly cache (GAC).
Unlike the private assemblies, this cannot be done by simply copying the assembly into appropriate folder – it needs to be specifically installed into the cache. This process can be performed by a number of .NET utilities and involves carrying out certain checks on the assembly.

What are the ways to deploy an assembly?
· An MSI Installer,
· A CAB archive, and
· XCOPY command.

What is a satellite assembly?
When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized modules that modify the core application are called satellite assembly.
A Satellite Assembly is defined as an assembly containing localized resources for another assembly.

What namespaces are necessary to create a localized application?
· System.Globalization
· System.Resources

What is the smallest unit of execution in .NET?
An Assembly.

How do you avoid the risk of name collisions in shared assemblies?
To avoid the risk of name collisions, shared assemblies are given a name based on private key cryptography (private assemblies are simply given the same name as their main file name). This name is known as a strong name, is guaranteed to be unique, and must be quoted by applications that reference a shared assembly.

How do you avoid the risk of overwriting an assembly?Problems associated with the risk of overwriting an assembly are addressed by specifying version information in the assembly manifest and by allowing side-by-side installations.

No comments: