Tuesday, July 1, 2008

create global assembly

Global Assembly:
.NET allows us to store and use different versions of same assembly without any problems
means successfully overcome dll hell problem.

The default system folder where your gac reside at:
c:/windows/system32/assembly

you can use an utility form dos prompt to view / list of all assemblies

prompt > gacutil /l ==> /l for listing

to make any .dll file in this directory u can use the same utility

prompt > gacutil /i yourfile.dll ==> /i means install



The conditions to make your dll as available in gac:

1. Provide strong naming
2. Should have a unique version.

Steps to create a global assembly:


create a file test.cs

using System.Reflection;
[assembly:AssemblyKeyFile("app.snk")]
[assembly:AssemblyVersion("1.0.0.1")]
public class test
{
public static void t1()
{
System.Console.WriteLine("global assembly");
}
}


prompt > sn - k app.snk //way to create strong name key file
prompt > csc /t:module /out:bin\test.dll test.cs
prompt > al /t:libary /out:bin\ga.dll test.dll
prompt > gacutil /i ga.dll

Now at runtime any one can refer this dll for their application.

create private assembly

Assembly : The intermediate file created by different language compilers in .NET environment are called as Assembly.

The other names:

1. MSIL (Microsoft Intermediate Language)
2. CIL (Common Intermediate Language)
3. IL ( Intermediate Language)
4. Manifest (technical name of assembly)
and also called as METADATA.

Assembly is actually a single .dll or .exe file or collection of .dll files.

It is of two types: PRIVATE ASSEMBLY , GLOBAL ASSEMBLY.


Private Assembly is a .dll file created which may have collection of different .dll files created by different languages stored in hard disk. Whenever the user want to include the dll file for their own applications they can use it at runtime as a reference. It need not be publically used by all users in a system. If you want to make your assembly can be used by everyone who are all logging into the system then you have to store in the folder: c:\windows\system32\Assembly.

This can be done using .net utilities called gacutil (global assembly cache utility

Step by Step procedure to create a private Assembly:

1. Create a .vb file convert this into .dll file
2. Create a .cs file convert into .dll file.
3. Create an application which calls two dll files at runtime.
4. Combine two dll files together (vb.dll and cs.dll) and make single .dll then refer it by application (Private Assembly)

1. Creating a c1.vb file as follows:

module c1
public shared sub m1() // shared means "static " in vb.net
system.console.writeline("vb")
end sub
end module

create .dll from the source file as:

.net prompt> vbc /t:module /out:bin\c1.dll c1.vb

//create a bin folder in the current directory
// vbc is vb.net compiler /t: means what is your target file
// the target may be module (single dll), libary (coll. of dll) or exe (executable file)
// /out: means my output file stored in the name c1.dll in bin folder

2. Create the .cs file and repeat the same step

using System;
class c2
{
public static void m2()
{
Console.WriteLine("method from csharp ");
}
}

create .dll dot net prompt > csc /t:module /out:bin\c2.dll c2.cs


3. Create an application

class app
{
public static void Main()
{
System.Console.WriteLine ("this is from application");
c1.m1(); // call method from vb program c1.dll
c2.m2(); // call method from cs program c2.dll
}
}

prompt> csc /t:exe /addmodule:bin\c1.dll /addmodule:bin\c2.dll app.cs

4. Combine two .dll files together and create private assembly using assembly linker [al]

prompt> al out:bin\pa.dll /t:libary bin\c1.dll bin\c2.dll


5. Use private assembly at runtime instead of adding individual modules:

prompt> csc /t:exe /out:bin\app.exe /r: pa.dll app.cs

/r: is the reference at runtime

the single dll file named pa.dll has the reference of two dll files (hash key reference)

Hope u successfully created a private assembly and understood the concept of language integration)

Common Language Runtime

CLR is an unified environment for all the languages in .NET means there is no difference in writing code as VB.NET , C#.NET, VJ#.NET, PERL.NET,COBOL.NET,MANAGED C++.NET,.....

I would like to give a small scenario how CLR works:

Java programmers can compile their source like

java source ==> Java Compiler ==> Byte Code ==> JVM ==> Executable Code

ex.java ==> javac ==> ex.class ==> JVM ==> binary code

Now the byte code here is called as Intermediate code which can run 0n any OS .
In the same way

c# code vb code vj# code perl.net code


c# compiler vb compiler vj compiler perl compiler



INTERMEDIATE CODE (CIL) : common intermediate language


CLR


JIT Compilation


EXECUTABLE CODE

here the Intermediate code in the source for CLR which gives the binary code.
The main components in CLR as follows:

Class Loader, IL to native compiler, code manager, Garbage Collector, security engine, debug engine, type checker, exception manager, Thread management, COM marshaler.

the part of JIT Just-in-Time compiling is part of IL to native compiler.

dot net framework

It is a part of modern windows operation system.
The runtime environment is CLR (Common Language Runtime) which is similar to Java JVM but with an added feature Language Integration which is againt Java.

Language Integration:

It is not the same meaning as Language independent. Language Integration can handle dll files and overcome the 'dll hell' problem. As more than one and half dozen languages supports .NET framework , its a great idea of accessing components created by other languages and also can catch exceptions generated by dll at runtime.

CLR : It has two sub blocks called CTS and CLS
i.e., Common Type System and Common Language Specification

Even though using different languages, the conversion of intermediate file has common notation called managed source code supported by .NET environment.

say a simple example,
you are declaring a variable in VB.NET as follows: dim i as Integer

the same can be done in C# as : int i ;

CLR understands these two different declarations in a common way as : System.Int32

So, the ultimate aim of dot net framework provides the support in the runtime environment like:
Language Integration, Language Independent, Platform Independent, Extensibility, strong naming, versioning, etc.,

The CLR operations to be disscussed.