You are on page 1of 7

Assemblies:

-An Assembly is a unit of file that contains the IL Code corresponding to a project
when it was compiled.

-The name of an assembly will be same as the project from which it was created.

-Assemblies are known as units of deployment because once the application


development is completed what we install on the clients machines is the assemblies
only.

-An assembly file can have an extension of either exe or dll.


exe: executable dll: dynamic link library

-Exe assemblies are known as in-process components which are capable of running on
their own as well as provide the support for others to execute. When we use the
project templates like Windows Forms, Console, WCF Applications & Windows Services
they create u an exe assembly.

-Dll assemblies are known as out-process components which are not capable of
running on their own they can only support others to execute. Project templates
likes Class Library and Control Library will create u a dll assembly.

-Assemblies are of 2 types:


-Private Assemblies
-Shared Assemblies

Private Assemblies:
-By default every assembly is private, if u add the reference of these assemblies
to a project, a copy of the assembly is created and given to the project. So each
application maintains a private copy of the assembly.

Creating a Private Assembly:


-Open a new project of type "Class Library" and name it as "PAssembly" which will
by default create you a class under it as Class1.
Note: A class library is a collection of classes, interfaces, structures etc., in
it, but cannot be executed as it was a dll.

-Now under the Class1 write the following code:


public string SayHello()
{
return "Hello from a private assembly";
}
-To compile the project open the Solution Explorer right click on the project and
select "Build" which will generate u an assembly with the name PAssembly.dll.

Note: You can find the path of the assembly which is created in the output window
present at bottom of the studio after build.

Consuming the Private Assembly:


-Open a new project of type Windows and name it as "TestPAssembly", place a button
on the form, set the text as "Call SayHello from PAssembly".

-Now add the reference of PAssembly.dll from its physical location and then write
the following code under the click of the button:

PAssembly.Class1 obj = new PAssembly.Class1();


MessageBox.Show(obj.SayHello());

-Run the project, test it and then go verify under the bin/debug folder of the
current project where u can find a copy of PAssembly.dll as it was private.

-The advantage of a private assembly is faster in execution as it was in the local


folder of project consuming it, where as the disadvantage is multiple copies gets
created when multiple projects add's the reference.
-----------------------------------------------------------------------------------
-
Shared Assemblies:
-An assembly which resides in a centralized location and provides the resources for
all the project who want's to consume it is known as a Shared Assemblies. The
centralized location was known as GAC (Global Assembly Cache).

-GAC is a folder that is present under the OS folder. <drive>:\Windows\assembly


<- GAC Folder

-All the BCL were assemblies of type dll and Shared so by default u can find all
those assemblies under the GAC.

-If an assembly is shared multiple copies will not be created even if being
consumed by multiple projects, only the single copy under the GAC serves all the
projects.

-What assemblies can be copied in to the GAC ?


Ans: We can copy only strong named assemblies in to the GAC.

-What is a Strong Named Assembly ?


Ans: An assembly which contains 3 attributes like Name, Version & Public Key Token
value is known as a Strong Named Assembly.

1. Name: It is the Name of the assembly for identification, every assembly by


default has a name. eg: PAssembly.dll.

2. Version: Every software has versions to it, for discriminating the changes that
has been made from time to time, as an assembly is also a software component it
will also maintain versions to it, when an assembly is created it is associated
with a default version i.e. 1.0.0.0, which can be changed when required.

3. Public Key Token: As GAC can contain multiple assemblies in it to identify each
assembly unique it will maintain a unique key value for each assembly known as
public key token, which has to be to generated by us and associate with the
assembly to make it Strong Named.

-How to generate a Public Key Token to associate with an assembly for making it
Strong Named?
Ans: To generate a Public Key Token value .Net provides us a tool known as Strong
Name Utility, which is a command line tool that should be used from the visual
studio command prompt as following:
sn -k <file name>
eg: C:\CSharp7> sn -k Key.snk
Note:we use either sn or snk extension for keyfiles.
-The above stmt generates a key value and stores it into the file "Key.snk".
-----------------------------------------------------------------------------------
--
Creating a Shared Assembly:
Step 1: Generate a key file.
-Open the visual studio command prompt and go into your own folder and generate a
key file as following:
C:\CSharp7> sn -k key.snk
Step 2. Create a project and associate the key file to it before compilation so
that the assembly which is generated will be Strong Named.

-Open a new project of type "Class Library" and name it as "SAssembly".

-Now under the Class1 write the following code:


public string SayHello()
{
return "Hello from shared assembly 1.0.0.0";
}
-To associate the key file to the project open the project property window and
select the "Signing" tab on the LHS which displays u a CheckBox "Sign the Assembly"
select it, which displays a ComboBox below it, from it select browse and choose the
key.snk file we generated, from its physical location which will add the file under
the solution explorer, now compile the project using the "Build" options which will
generate u an assembly as SAssembly.dll which is Strong Named.

Step 3: Copy the Strong Named Assembly to GAC.


-To copy the assembly into the GAC .Net provides us a utility known as "gac
utility" which is a command line tool that has to be used as following:
gacutil -i | -u <assembly name>
-i Install
-u Uninstall
-Open the Visual Studio command prompt and go into the location where our
SAssembly.dll is present and write the following:

C:\CSharp7\SAssembly\SAssembly\bin\Debug> gacutil -i SAssembly.dll

Testing the Shared Assembly:


-Open a new project of type Windows and name it as "TestSAssembly", place a button
on the form and set the text as "Call SayHello of SAssembly 1.0.0.0".

-Add the reference of the SAssembly.dll from its physical location and then write
the following code under the click of the button:

SAssembly.Class1 obj = new SAssembly.Class1();


MessageBox.Show(obj.SayHello());

-Run the project, test it and then go verify under the bin/debug folder of the
current project where u will not find a copy of the SAssembly.dll as it was shared.

Note: When a shared assembly is used in multiple applications there will not be
multiple copies of the assembly, where as it is not faster in access when compared
with a private assembly.
-----------------------------------------------------------------------------------
--
Assembly Attributes:
-Every assembly is associated with a set of attributes like Title, Company,
Description, Version etc., which describes about the general information of an
assembly.

-These attributes can be found under a file AssemblyInfo.cs under each project, to
view it in the solution explorer expand the node Properties under the Project below
it u can find the AssemblyInfo.cs file open it to view the attributes of an
assembly.
Note: U can change any attribute according to your requirement.

-In the bottom u find an attribute known as version which is a combination of 4


values like:
-Major Version
-Minor Version
-Build Number
-Revision
-The default version number associated with every assembly will be 1.0.0.0, to
change it we following the below guidelines:

-Change the revision number when modifications were made to the existing members of
the assembly.

-Change the build number when new members were added under the assembly.

-Change the minor version when modifications were made to the existing classes of
the assembly.

-Change the major version when new classes were added under the assembly.

Testing the process of changing version numbers:


-Open the SAssembly project we created previously and change the code under the
SayHello method as following:
return "Hello from new version of shared assembly 1.0.0.1";

-Now open the AssemblyInfo.cs file and change the AssemblyVersion and
AssemblyFileVersion attributes as 1.0.0.1 in the bottom of the file.

-Re-build your project and add the new version of the SAssembly also under the GAC
using the GACUtil tool.

Note: GAC allows placing of multiple versions of the same assembly into it and
allows different application using different versions of the assembly to execute
correctly using their required version.

-Open the GAC folder where u can find the 2 versions of the SAssembly i.e. 1.0.0.0
& 1.0.0.1

-After placing multiple versions of the assembly under the GAC u can develop
applications in such a way where a different application can use a different
version and still execute correctly what we call as side by side execution of
application which was not possible in the tradition COM technologies.

-To test this open a new project of type Windows and name it is TestNewSAssembly,
place a button on the form and set the text as "Call SayHello of SAssembly
1.0.0.1".
-Add the reference of the 1.0.0.1 version of the SAssembly from its physical
location and write the following code under the click of the button:

SAssembly.Class1 obj = new SAssembly.Class1();


MessageBox.Show(obj.SayHello());

-If you want to test the side by side execution of the projects run the exe files
corresponding to the TestSAssembly and TestNewSAssembly projects at the same time,
where each project will use a different version of SAssembly and executes as
following:
-----------------------------------------------------------------------------------
--
-An Assembly internally contains the following 3 things in it:
-Manifest Info
-Type Metadata
-IL Code
-Manifest Info contains the information of the attributes that are associated with
the assembly like Title, Description, Company, Version etc.,

-Type Metadata contains the information of the Namespaces, Classes & their members
(only signature). The Type Metadata only describes about the contents of the
assembly where an assembly can be called as self describing units of execution as
it describes about it self to the execution environment with the help of the
metadata.

Note: When u add the reference of any assembly to the visual studio, first it will
read the information of the assembly from its type metadata only.

-IL Code is the language instructions we have written that can be understood by the
CLR.
-----------------------------------------------------------------------------------
--
COM: Component Object Model
-It is a specification prescribed by Microsoft which advices never build a software
as a monolithic (single) unit, in spite it advices building of the software by
dividing into multiple components and then integrate as a software which provides
the following features:
i) Software maintanance becomes easier.
ii) Reusability of the components in multiple softwares.
iii) Language Independency.

-Inspite of the features, it also suffers with few drawbacks:


i) Platform dependent which works only on windows.
ii) COM components (dll or exe) has to be first registered under the OS to
consume on a machine.
iii) Versioning problem or dll Hell.

-To overcome the above problems in COM Microsoft in it's .Net replaced COM with
Assemblies which were Platform Independent that doesn't require any registration on
the OS as well as solves the versioning problem's also.
-----------------------------------------------------------------------------------
--
COM Interoperability:
-After replacing COM components with .Net assemblies Microsoft has given a
flexibility to the industry i.e. consuming of COM components under .Net application
development and .Net assemblies under COM application development which we call as
COM Interoperability.

-A COM component can be consumed under a .Net application by first getting


converted into RCW (Runtime Callable Wrapper) which exposes all the COM interfaces
as .Net interfaces to the .Net application.

-CCW (COM Callable Wrapper) is opposite of RCW that exposes .Net interfaces as COM
interfaces to a COM application.
-----------------------------------------------------------------------------------
--
Creating a COM Component:

-COM Components can be developed using various languages like classical VB, C++ &
VCPP.

-To develop a COM Component in Classical VB goto Start Menu -> Programs ->
Microsoft Visual Studio 6.0 -> Microsoft Visual Basic 6.0, click on it to open
which displays a "New Project" window, select ActiveXDll (Class Library in .Net)
project template and click open which displays the code window.

-Goto View Menu select "Project Explorer" which launches the explorer on the RHS,
in it right click on the node Project1 and select properties which opens u a window
under that change the "Project Name" as CSharp7Com and click Ok.

-Now right click on the node Class1 and select properties and change the name as
"Test".

-Now under the code window write the following code:


Public Function SayHello() As String
SayHello = "Hello From COM"
End Function

Note: Classical VB language doesn't supports the return statement so the value that
has to be returned should be assigned to the name of the function which will return
the value.

-Now go to the File Menu and select Save Project and specify the location where u
want to save the project. eg: C:\CSharp7\VB

-Again go to the File Menu and select the option Make CSharp7Com.dll, which will
compile the code, generates a dll and registers it under the windows registry as
COM Components were not functional unless they were registered under the OS.

-Close the Studio.


-----------------------------------------------------------------------------------
--
regsvr32 <name of com dll>
-To manually register the com component under the os registry, use the command from
the command prompt, from the folder where the com component was present.
-----------------------------------------------------------------------------------
--
Consuming the COM Components from .Net App's:
-If we want to consume a COM Component from a .Net Application first it should be
converted into RCW which can be done in 2 ways:
-Using Visual Studio.Net
-Using tlbimp utility

Generating RCW using Visual Studio.Net:


-Open a new .Net project of type Windows and name it is as TestCom1, place a button
on it and set the Text as "Call SayHello From COM".

-Now open the "Add Reference" window and under the COM tab u can find out your COM
Component "CSharp7Com" select it and write the following code under the click of
the button:

CSharp7Com.TestClass obj = new CSharp7Com.TestClass();


MessageBox.Show(obj.SayHello());

-Now run the project, test it and then go verify under the debug folder of the
current project where u find out the RCW which is created by VS as
"Interop.CSharp7Com.dll".

-The above process is simple and easy but the RCW created here is private which
maintains multiple copies if used under multiple projects.
-----------------------------------------------------------------------------------
--
For creating an RCW Or CCW .Net provides you 2 command line utilities:
tlbimp (Type Library Importer): used for converting a com component into RCW

tlbexp (Type Library Exporter): used for converting a .net assembly into CCW
-----------------------------------------------------------------------------------
--
Generating RCW using tlbimp Utility:
-VS is capable of generating RCW of type private only where as tlbimp is capable of
generating both private and shared RCW's.

-It is a command line utility which should be executed from VS Command Prompt as
following:

tlbimp <name of com dll> /out: <name of .net dll> [/keyfile: <name of key file>]

Creating a Private RCW:


C:\CSharp7\VB> tlbimp CSharp7Com.dll /out: ComToNetP.dll

Creating a Shared RCW:


Step 1: Generate a key file
C:\CSharp7\VB> sn -k Key.snk

Step 2: Converting to Shared RCW


C:\CSharp7\VB> tlbimp CSharp7Com.dll /out: ComToNetS.dll /keyfile: Key.snk

Step 3: Add the Shared RCW to GAC


C:\CSharp7\VB> gacutil -i ComToNetS.dll
-----------------------------------------------------------------------------------
--
Test Com2:
ComToNetP.TestClass obj = new ComToNetP.TestClass()
MessageBox.Show(obj.SayHello());

Test Com3:
ComToNetS.TestClass obj = new ComToNetS.TestClass()
MessageBox.Show(obj.SayHello());
-----------------------------------------------------------------------------------
--
tlbexp is a tool that can convert a .net assembly into CCW, this is opposite to
tlbimp.

tlbexp <name of .net dll> /out: <name of com dll>

-After creating the CCW it cannot be used directly under any system as it was a COM
component it requires registration on the OS which can be done using the following
stmt:
regsvr32 <name of CCW>

You might also like