Friday, May 11, 2012

Types of Assemblies


Types of Assemblies


As Assemblies are buliding blocks of Dotnet Frame work there are 3 types of Assemblies
1. Private
2. Shared
3. Satellite

Private AssemblyThe Assembly that is local to the particular folder is called Private Assembly
The disadvantage is that each time a copy of the Assembly is created which in turn occupies more memory and reduces the performance

Shared AssemblyWhen we maintain one copy of the Assembly for set of applications through out the system it is called Shared Assembly
More memory usage can be reduced by this concept

So here comes the concept of public key and version number to maintain unique ness of the Assembly for particular application.

Satellite AssemblyWhen you wish to make the application customizable for different languages and cultures and distribute the localized modules in separate assemblies called satellite assemblies.
It can be written in code that uses resources from an assembly rather than hard-coding the resources in the program

Private Assembly


When we compile the C# Program Console Application an exe will be created

To Create dll from the Command Prompt


Open Note pad and Type the Following
Example:
namespace MyApp
{
public class MathC
{
private int a,b;
private int res;
public void Accept( int A,int B)
{
a =A;
b= B;
}
public int Sum()
{
res = a+b;
return res;
}
}
}

Save it as Mydll.cs

Compilation : csc /t:library Mydll.cs

Mydll.dll is Produced


Calling this assembly in Client Application


using System;
using MyApp;

class C{
static void Main()
{
MathC obj = new MathC();
obj.Accept(10,20);
Console.WriteLine("Obj value is"+obj.Sum());
}
}

Save it as ClientApp.cs

If we compile csc ClientApp.cs it gives error

The correct compilation is csc /r: mydll.dll ClientApp.cs

/r refers to reference switch.
It’s a request to the complier to verify name spaces (all) with in System.dll by default. When we want to provide a custom library to verify the namespace then we require reference switch option

Hence this will produce ClientApp.exe
And the result is executed

C:\>ClientApp
Obj value is30

C:\>md Dir1

C:\>copy PAssembly.dll Dir1
1 file(s) copied.

C:\>del PAssembly.dll

C:\>ClientApp

LOG: Attempting download of new URL file:///C:/PAssembly.DLL.
LOG: Attempting download of new URL file:///C:/PAssembly/PAssembly.DLL.
LOG: Attempting download of new URL file:///C:/PAssembly.EXE.
LOG: Attempting download of new URL file:///C:/PAssembly/PAssembly.EXE.

Run Time will search for Mydll.dll in C Drive
And gives the Error Message

Probing


Probing can be defined as Runtime Searching for the Assembly required to the Application
When it comes to private assembly Runtime will search for Application Folder and subfolder with in a library name for the particular assembly
When we want to extend probing with other directories then provide the information to the runtime in a special file called Configuration file

Save it as ClientApp.exe.Config


<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<probing privatePath="Dir1"/>

</assemblyBinding>

</runtime>
</configuration>


assemblyBinding Contains information about assembly version redirection and the locations of assemblies.
configuration The root element in every configuration file used by the common language runtime and .NET Framework applications.
runtime Contains information about assembly binding and garbage collection.

Now go to VS Command Prompt and just run ClientApp
Then It will search for the Assembly in the Located path as Dir1 in the Specific Root Directory and all the Subfolders

We get the output as : Sum is 30

Built in Library vs Custom Library


Built in Library defined by the MS comes from System NameSpace
Console Class

Custom Library defined by the Developer that is Mydll
MathC is the class
Import the name space with ‘using’ keyword

No comments:

Post a Comment