Vous êtes sur la page 1sur 7

Assemblies in .

Net
When we create a project in Visual Studio .Net and compile it, assemblies are created. These assemblies are the fundamental units of applications in the .NET Framework. An assembl can contain classes, structures, interfaces, and resources that an application re!uires. When we compile a pro"ram, the compiler translates the source code into the Intermediate Language code (IL). #n addition to translatin" the code into #$, the compiler also produces metadata about the pro"ram durin" the process of the compilation. %etadata contains the description of the pro"ram, such as the classes and interfaces, the dependencies, and the &ersions of the components used in the pro"ram. The #$ and the metadata are linked in an assembl . An #$ and metadata e'ist in portable executable file(E)E*+$$,. #f we look at an project folder, we will find a bin folder that contains, sa m project.dll* m project.e'e and m project.pdb files. The m project.dll* m project.e'e is the assembl , and m project.pdb ( pro"ram database , file contains debu""in" information for the assembl . The .pdb file also contains information the linker can use when debu""in" the assembl . So, when we create and compile the project in VS.Net, the assembl is automaticall "enerated and copied into the application-s bin director . An assembl also contains an assembly manifest that contains the assembl metadata. This metadata contains information about the assembl &ersion, its securit identit , the resources re!uired b the assembl and the scope of the assembl . This information is stored within the assembl file (+$$*E)E, itself. Note that the assembl contains two t pes of metadata. .ne, is the t pe metadata and the other is the assembl metadata.

Process assemblies (EXE) and library assemblies (DLL)


A process assembl represents a process which uses classes defined in librar assemblies. The compiler will ha&e a switch to determine if the assembl is a process or a librar and will set a fla" in the /E file. .NET does not use the e'tension to determine if the file is a process or librar . This means that a librar ma ha&e either .dll or .e'e as its e'tension. The .Net 0$1 while e'ecutin" looks for the metadata in order to locate and load classes, as well as "enerate nati&e code and securit . Note that the assembl metadata is assembl manifest and it simplifies the deplo ment model. 2ecause it contains so much information about the assembl , ou can simpl )0./3 the assembl onto another computer, alon" with its related manifest.

!y use Assemblies"
The "oal of the assembl model is the elimination of +$$ 4ell. 5nder the current 0.%*0.%6 model, a catalo" of +$$s is centrali7ed in the Windows 1e"istr . When a new &ersion of a +$$ is published, the re"istr re8references the catalo" to point to the new +$$. This centrali7ed re"istration paradi"m makes it challen"in" for multiple applications to depend on the same +$$. %ost often, the application binds to a +$$ in a centrali7ed location, rather than run multiple &ersions of a component b usin" side8b 8 side e'ecution. The .NET Framework makes it eas to run multiple &ersions of a component because it stores assemblies in local application directories b default. This isolates the assembl from use b an other application and protects the assembl from s stem chan"es.

#ypes of Assemblies
Assemblies can be pri$ate or s!ared. The assembl which is used b a sin"le application is called as pri$ate assembly. Suppose we ha&e created a +$$ which encapsulates business lo"ic. This +$$ is used b the client application onl and not b an other application. #n order to run the application properl the +$$ must reside in the same folder in which the client application is installed. Thus the assembl is pri&ate to the application. Suppose we are creatin" a "eneral purpose +$$ which pro&ides functionalit to be used b a &ariet of applications. Now, instead of each client application ha&in" its own cop of +$$ we can place the +$$ in -"lobal assembl cache-. Such assemblies are called as s!ared assemblies. When we create an application in Visual Studio.Net that uses a component, which is not in the 9A0, the assembl is automaticall copied to the project:s bin folder. So, if we simpl the cop the project:s bin folder, the assembl for the component is copied as well. Since the files the application needs are in the same folder, the application can run without re"istration.

%enefits of Pri$ate Assemblies


/ri&ate assemblies are installed in a director named bin located under the application director . These files are pri&ate to the application. No &ersionin" is re!uired, as lon" as it is the same &ersion as the one with which the application was built. #t enables )0./3 deplo ment There is no confi"uration or si"nin" #t is "reat for small utilit Assemblies* application specific code. Disad$antages&

When ou ha&e multiple applications usin" one assembl , ou ha&e to deplo the assembl to the bin director of each application.

'o( to create an Assembly


A shared assembl can be created in Visual Studio.Net b "eneratin" 0r pto"raphic ;e /air usin" the tool SN.e'e utilit . The sn.e'e utilit is located in the 2in director . After creatin" the shared assembl we should si"n the shared assembl to the 9A0 b usin" 9acutil tool. %ore about creatin" a shared assembl .

!at is )lobal Assembly *ac!e"


An assembl that is shared b multiple applications is called a "lobal assembl and is installed in the global assembly cac!e ()A*). 9lobal assembl cache is nothin" but a special disk folder where all the shared assemblies are kept. #t is located under <dri&e=>?Windows?Assembl folder on e&er machine runnin" the .NET framework. 9A0 acts as a central location for assemblies that is accessible b an application.

+ummary&
Assembl ph sicall e'ist as +$$s or E)Es. An assembl consists of a manifest and the portable e'ecutables (/E,. 9lobal assembl cache is nothin" but a special disk folder where all the shared assemblies will be kept.

'o( do I create s!ared assemblies "


4ow to install an assembl in the 9lobal Assembl 0ache in Visual 2asic .NET or in Visual 2asic @AABC A s!ared assembly is one that is used b multiple applications on the machine. A shared assembl must ha&e a name that is "loball uni!ue. The .NET Framework supports these namin" re!uirements throu"h a techni!ue called stron" names. Shared assemblies must ha&e a Dstron" nameD consistin" of the name of the assembl , the &ersion, a EF bit hash of a public ke (called a :token:, and the culture. To create a stron" name for an assembl (that is, to "enerate a public*pri&ate ke pair,, ou-ll ha&e to use another utilit called sn.e'e. #n this article, we will create a ,isual %asic -../ component called m 9A0. We will also create a ke file named m ke s.ke . We will si"n our component with this ke file and place it in

)lobal Assembly *ac!e


To create a *omponent we ha&e to create a *lass Library pro0ect b usin" Visual Studio .NET.

+tep 1 & 2pen ,isual +tudio .NE#


0reate a new 0lass $ibrar project named m 9A0 in Visual Studio .NET or in Visual 2asic @AAB. 4ere is the code for the component. #t just includes one method which returns a strin".
imports system namespace BuildGAC public class HelloClass public function GetData() as string return hello, how are you doing end function end class end namespace

+tep - & )enerating *ryptograp!ic 3ey Pair using t!e tool +N.Exe
Now, we will use the SN tool to "enerate the cr pto"raphic ke pair, which is located in the ?bin subdirector , where the .NET Framework Solution +e&eloper ;it (S+;, is installed. We will open the +.S prompt. The command8line statement format is> sn 8k D0>?G+irector To/lace;e H?G;e NameH.ke D Ne't, create a director named m ke s in 0>? so that ou can easil locate the ke and access the ke from the command prompt. N2#E& For most users, the .NET tools are located in 0>?/ro"ram Files?%icrosoft.NET?FrameworkS+;?2in. T pe the followin" at the command prompt. sn 8k D0>?m ke s?m ke s.ke D

+tep 4 & +ign t!e component (it! t!e 5ey


A ke is "enerated, but it is not et associated with the project-s assembl . To establish the association, double8click the Assembl #nfo.&b file in the Visual Studio .NET or Visual Studio @AAB Solution E'plorer. Add the followin" to the list of assembl attributes that are included in this file b default when a project is created in Visual Studio .NET or in Visual Studio @AAB> <Assembl > Assembl ;e File(D0>?m ke s?m ke s.ke D,=

Now recompile the project and the assembl will be si"ned. 2elow is a listin" of the complete code of assembl Assembl #nfo.&b #mports S stem.1eflection #mports S stem.1untime.#nteropSer&ices <Assembl <Assembl <Assembl <Assembl <Assembl <Assembl <Assembl <Assembl > Assembl Title(DD,= > Assembl +escription(DD,= > Assembl 0ompan (DD,= > Assembl /roduct(DD,= > Assembl 0op ri"ht(DD,= > Assembl Trademark(DD,= > 0$S0ompliant(True,= > Assembl ;e File(D0>?m ke s?m ke s.ke D,=

<Assembl > 9uid(DEIFJ@AE@8BIKJ8F@KE8JFAI8LIIL00@JEAE+D,= <Assembl > Assembl Version(DL.A.MD,= The ne't step is to install the assembl into the 9lobal Assembl 0ache.

+tep 6 & 'ost t!e signed assembly in )lobal Assembly *ac!e


There are two wa s to install an assembl into the "lobal assembl cache>

7sing 8icrosoft

indo(s Installer -..

This is the recommended and most common wa to add assemblies to the "lobal assembl cache. The installer pro&ides reference countin" of assemblies in the "lobal assembl cache, plus other benefits.

7sing t!e )lobal Assembly *ac!e tool ()acutil.exe)


3ou can use 9acutil.e'e to add stron"8named assemblies to the "lobal assembl cache and to &iew the contents of the "lobal assembl cache. Note& 9acutil.e'e is onl for de&elopment purposes and should not be used to install production assemblies into the "lobal assembl cache. S nta' to install a stron"8named assembl into the "lobal assembl cache 3ou can install the .dll file b usin" the 9acutil Tool or b dra""in" the .dll file into the appropriate director . #f ou use the 9acutil Tool, ou can use the followin" command> At the command prompt, t pe the followin" command> "acutil # <assembl name=

#n this command, assembl name is the name of the assembl to install in the "lobal assembl cache. The followin" e'ample installs an assembl with the file name m 9A0.dll into the "lobal assembl cache. "acutil 8# D0>?G/athTo2in+irector #nVS/rojectH?m 9A0.dllD After hostin" the assembl just "o to W#NNT?Assembl folder and ou will find our assembl listed there.

+tep / & #est t!e assembly


.nce ou-&e installed the assembl into the 9A0, ou can use it from other pro"rams b creatin" a reference. Now, we will create a sample client application which uses our shared assembl . #n our sample client9A0Test project, e'pand the project and then ri"ht8click on the 1eferences item. Select DAdd 1eferenceD from the popup menu, and the Add 1eference dialo" bo' is displa ed. To refer an assembl in the 9A0, just click on the 2rowse button and browse to the director (m 9A0*bin*debu", that contains the assembl (m 9A0.dll,. $ocate the assembl , select it, and click .;. 0lick on the m 9A0 reference in Solution E'plorer and then e'amine the /roperties pane. 3ou-ll see that the 0op $ocal propert is set to False, Stron" Name is true, and that the assembl &ersion is L.A.@IEJ.INLJJ Oust create a sample code as listed below > 3ou can now compile and run the client9A0 pro"ram.
!mports "ystem !mports myGAC#BuildGAC $ublic Class "ample%est "hared "ub main() Dim & As 'ew HelloClass() Dim s As "tring ( &#GetData() Console#)rite*ine(s) +nd "ub +nd Class

Now, cop the resultin" E)E in an other folder and run it. #t will displa D4ello WorldD indicatin" that it is usin" our shared assembl .

+ummary&
This article describes how to "enerate a stron" name for an assembl and to install a .dll file in the 9lobal Assembl 0ache. The 9lobal Assembl 0ache (9A0, enables ou to share assemblies across numerous applications. The 9A0 is automaticall installed with the .NET runtime. 0omponents are stored in 0>?W#NNT?Assembl .

Vous aimerez peut-être aussi