hello welcome to presume technologies I
am venket this is part four of dotnet
basics in this session we'll understand
what GAC is how and when to install an
assembly into GAC now
GAC stands for global assembly cache and
it contains all these stronger named
assemblies the advantage of installing
an assembly into the GAC is that you
know that specific assembly can be
shared with multiple applications
without having to have a copy of that
assembly in each of the projects where
you want to use it let me explain this
with an example now let's say I have
this control application all it does is
print out this message onto the console
and to this solution I want to add
another project okay let's say I want to
add a class library project so we shall
see shop class library and let's call
this class library project and click OK
so this should add this class library
project within the solution and let's
say within this project I have a sample
class which has got a static method that
returns a string so public static string
and then get name maybe which returns a
simple string so written a string like
this
all right so every simple assembly class
library project now obviously when we
build this project as you might expect
we have an assembly generated and I want
to use that assembly within this project
now if you look at the console
application within this console
application we are using the console
class that is present in the system
assembly so where is the system assembly
coming from from the references folder
if you look at the reference here you
know this project has a reference to the
system assembly now where is the system
assembly coming from now keep in mind
this is a dotnet framework assembly and
when we install dotnet framework on this
machine
this assembly got installed into the CAC
okay we will see where it is present in
a bed okay so basically if you want to
use an assembly external to this project
then you have to add a reference to that
assembly and to do that
you right click on the references folder
add reference and basically there are
two ways to add a reference to this
project this class library project okay
we will talk about that in a great
detail in a later session but for now
understand that if I have to add a
reference to this class library project
then I go into the Browse tab and we
know that this project is present in
introduction to c-sharp folder within
the C Drive so we go to introduction to
c-sharp folder within the C Drive and we
have the class library project get into
the bin-debug and then select the
assembly and the moment we do that that
gets added to the references so class
library project and now what has
actually happened when we added this
reference let's look at that so when I
open this project within Windows
Explorer okay so this is the console
applications project now obviously this
is introduction to C shop when we
compile that project we get an awesomely
for this project so when I get into the
bin folder
okay so I have introduction to c-sharp
assembly that's fine okay let's rebuild
this once again
okay so I have introduction to see shop
that's fine but if you look at this this
project bin folder introduction to see
shops project bin folder has also got
this class library copy of the assembly
so how did we get this so that's what
happened when you actually added a
reference to this class library okay so
so obviously if I have to use this
assembly let's say in ten different
projects and when you add a reference
what's gonna happen a copy of that
assembly is made into all of the ten
projects where you want to use that okay
but whereas if you install that assembly
into the GAC you don't have to do that
okay
so the reason why this assembly gets
copied into the bin folder of this
consuela application is because if you
look at the properties of that assembly
so there is a property called copy
looker which is set to true which means
copy that assembly locally into the bin
folder of this controller application
okay but if you set that to false then
it doesn't get you know copied into the
local directory but then there are other
implications which we'll talk about in a
later session but on the other hand if
you look at the system assembly when I
right-click on that and get to the
properties this copy local property is
set to false which means you know for
all the assemblies that are present in
the gag you know when you add a
reference to those assemblies by default
the copy local will be false which means
a copy of that assembly will not be made
into the bin folder of that particular
application so what's the advantage of
installing an assembly into the GAC
you can potentially share that assembly
with multiple applications without
having to have a copy of that assembly
in each of the projects where you want
to use that but again can we install an
assembly into the gag every time now
okay now if there is really a need to
install an assembly into the gag if you
think that it's gonna be potentially
shared by multiple applications then go
ahead and install that into the cache
but otherwise you shouldn't be doing
that okay so it's recommended to install
an ass
into the gag only when required and
shared by applications otherwise they
should be kept private now there is
another specific reason why you
shouldn't be installing an assembly into
the gag now you shouldn't add an
assembly into the gag if you wish to
deploy your application to another
machine using xcopy deployment now what
do we mean by xcopy deployment xcopy
deployment is basically simply copying
an application from one machine to
another machine or one location to
another mission location you just copy
the contents of that folder that
application folder so when you do that
only the contents I mean that some leads
and other files that are present within
that Applications folder gets copied
over to the new location or to the new
machine but the contents that are
present in the GAC they don't get copied
over so obviously the dependent
assemblies doesn't get copied over to
the target machine because of which your
application will not get properly
executed when you do an xcopy deployment
and when you have strong named
assemblies in the gag okay that's why if
you intend to do an X copy deployment
for your application then the assembly
should not be you know installed into
the GAC they should be kept private and
they should be present in the
application folder that is containing
the Exe okay all right so let's now talk
about the GAC itself now it's in its
turn it turns out that until the
introduction of dotnet 4.0 we only had
one version of the gag and the location
for the GAC until the introduction of
dotnet 4.0 is C colon backslash windows
directory backslash assembly now this
part may be slightly different from
machine to machine on some of the
machines you might have named the
windows installation directory as when
NT in that in that case the path for the
GAC is C even and T and assembly and if
you look at this this is what the CAC
cares
you have the assembly name version
number culture and public key token okay
now with the introduction of Dartmouth
4.0 we have another gag okay so if you
look at the slides here okay with the
introduction of mine dotnet 4.0 we have
two gags and one for dotnet 2.0 23.5
okay for all the versions from Rodney
2.0 to 3.5 all these version of the
assemblies get installed into this
version of the GAC C backslash windows
backslash assembly but if it's a dotnet
4.0 assembly then that gets installed
into this location so if somebody asks
in an interview you know what are what
is the path for GAC they're basically
two parts one for dotnet versions 2.0 to
3.5 and another one for dotnet 4.0
alright so now to install an assembly
into the gag you use a tool called GAC
utility global assembly cache utility
tool okay basically there are two ways
one of the ways is simply to drag and
drop you know from one folder to another
folder just drag and drop the assembly
it gets copied over or you can use this
GAC utility tool GAC util dot exe so
we'll see how to install an assembly
into the gag using this clack GAC
utility tool okay so now if you look at
this project we have this class library
project and you have already seen it has
generated an assembly for us so let's go
ahead and see that and try to install
that assembly into the GAC and keep in
mind this is Visual Studio 2010 so if
you are building an application using
Visual Studio 2010 by default we are
targeting dotnet framework 4.0 so this
assembly is making use of dotnet
framework 4.0 so it gets installed to
this new gap which is C Windows
microsoft.net backslash assembly okay so
now let's try to install this assembly
into the gag okay and to do that gack
utility is a command line tool so
obviously we need to get to the Visual
Studio command prompt so all programs
Microsoft Visual Studio 2010 Visual
Studio Tools Visual Studio command
prompt right click run as administrator
so this should bring a Visual Studio
command prompt now you know that
the part where we have our assembly is
this part so get copy that path and
change the directory to that path by
typing CD and then the part of the
directory and then we want to use CAC
utility here so CAC utility GAC yuto dot
exe again dot exe is optional here and I
want to install an assembly so - I so we
we specify our intention using this I
switch I want to install an assembly and
then the name of the assembly itself so
the name of the assembly that I want to
install is class library DLL so paste
that now when I press ENTER I get an
error but we'll solve that in a bit
so what is the error message that you
are getting failure adding assembly to
the cache attempt to install an assembly
without a strong name so obviously if
you want to install an assembly into the
GAC the assembly has to be strongly
named ok but if you look at this
assembly it's not strongly named and if
you remember in the previous session we
have discussed you know how to sign and
assembly you know using a private and
public key pair and then convert that
into a strong named assembly if you
haven't watched that video I strongly
encourage you to do so ok so if you
remember we have generated a key pair
using SN e XE tool in a previous session
so this is the key file which contains
our private and public key pair again if
you're not sure about this please watch
our previous video so copy the name of
the key file and we know that if we want
to sign the class library assembly with
this private public key pair we use the
assembly info dot CS file and within
that we use the assembly key file
attribute
so assembly keyfile attribute and to the
constructor of this attribute we pass in
the name of the key file that contains
our keys okay
and since backslash has a meaning we
need to escape that with another
backslash okay so now if I rebuild a
solution what's gonna happen this class
library assembly gets signed with this
private public key pair so now this
assembly is a strongly named assembly
and we can install that into the GAC
without any issues let's see how to do
that again so the same command GAC
utility dot exe - hi class library dot
dl ow so now assembly got added
successfully to the cache now so to
check if it is successfully added which
version of the GAC should we be checking
we should be checking the second one
because we are using Visual Studio 2010
and Visual Studio 2010 mix makes use of
dotnet framework Zero framework 4.0 by
default okay so let's get to that path
so it's present in C colon backslash
windows backslash microsoft.net
backslash assembly so within this path
you have another folder called gag msi l
microsoft intermediate language and if i
open that you should see this class
library folder here and within this
folder you will have your version number
basically we are targeting dotnet
version 4.0 so that's what you can see
underscore the version of the assembly
if you look at the version of our class
library assembly it's 1 dot 0 dot 0 dot
0 so that is that version here and then
another underscore and at the public key
token okay so this folder uniquely
identifies our class library dot DLL and
since this is dotnet framework 4.0 we
got that assembly installed into this
new CAC for dotnet 4.0 assemblies
ok now to uninstall the assembly from
the CAC you use you know the exact
click the same tool but you use a
different switch - I for installation -
you for an installation so I can use the
same command except that I need to use -
new and if I press Enter
the assembly doesn't get installed I'll
explain why in a bet so it says ok it
didn't give us any error it is just
saying number of assemblies and install
is equal to zero that's because when you
are uninstalling an assembly you cannot
provide this extension dot DLL so you
have to get rid of that so dot DLL and
when I press ENTER you see that one of
the assemblies is basically uninstalled
here ok
so if you look at this assembly it got
uninstalled so you don't see this that
folder class library folder here anymore
now as I told you by default if we have
Visual Studio 2010 we are targeting
dotnet framework 4.0 okay but it's
possible you can change the targeted
dotnet framework and the way we do that
is right click on the project and get
into the properties and now here you can
change the dotnet target framework now I
want to target let's say for example
dotnet 3.5 I select that you get a
warning just say s as a result of
switching you know from 4.0 to 3.5 some
of the references might get invalid but
that's fine let's go ahead and build our
project now so when I build our project
now what what have we done now we have
switched the target framework from 4.0
to 3.5 so obviously now if I try to
install this assembly into the GAC it
gets installed into our another you know
into the version which for which I mean
for dotnet basically 2.0 and 3.5
assemblies so which is C Windows
assembly so it gets installed into this
location let's see that so gakuto - i +
library dll when I press ENTER so
assembly successfully added to the cache
so we get to that version of the
assembly so it's present in C colon
backslash Windows
backslash assembly so you should see the
class library assembly here so the name
of the assembly the version number is
this one and this is the public key
token okay now let's do another thing
let me add let me change the version of
this assembly to 2.0 so by default the
version is 1.0 I'm changing the version
to 2.0 and then rebuild the solution and
then I'm trying to install this assembly
once again and let's see what's gonna
happen so now this assembly got
successfully added to the cache once
again and if we get into the windows I
mean into the GAC once again you should
see there two versions of class library
one is version 1.0 and the other one is
2.0 okay so now assemblies with the same
name but has got different version can
live side by side in the same folder and
this is called side by side execution
within dotnet assemblies with the same
name with the same public key token but
different versions can live together and
execute together
alright so and this feature is called as
I told you
side by side execution and this is one
of the common interview question what do
you mean by side by side execution
within dotnet or they can also ask you
can two different assemblies with the
same name live in the same folder of
course all right so now if I go ahead
and uninstall try to uninstall the
assemblies look at that when I say - you
and when I press enter now what's gonna
happen so you're saying okay uninstall
an assembly with name class library and
if you look at that I have got two
assemblies with that name so when I try
to execute this command what's gonna
happen
both the assemblies gets uninstalled
okay let's see that so when I press
ENTER so it's trying to uninstall both
of the assemblies so if you look at the
message here number of assembly is
uninstalled - okay let me say I don't
want both of the assemblies to be
uninstalled I want just one of the
assemblies may be version 1.0 to be
uninstalled is that possible
absolutely but you have to specify the
fully qualified name
okay so to uninstall an assembly to
uninstall its specific assembly using
GAC utility what you do I mean it's
exactly the same command except that you
specify the version number and public
key token as well okay so in this case
version 1.0 assembly alone will be
uninstalled okay but keep in mind when
you're using you know the fully full
name of the assembly make sure you don't
have a space between comma and the
letter V and similarly between this
comma and this letter P if you have
space it gives you an error message all
right on this slide you can find
resources for asp.net and C sharp
interview questions that's it for today
thank you for listening have a great day