Building From
Command Line
- In this example, we
will see how we are able to build an assembly and use it from command
line. This will also help us get a better understanding of how the
assemblies are used. We will look at two examples. The first is the use of
a private assembly, the second is the use of one
that has a strong name, a potential candidate for a shared assembly.
- First create a
directory named temp on your c:\ if one does not exist already.
- Then create a
directory named release under c:\temp.
- Open a command window
and cd to the CarComp
project directory. This can be easily done by following the steps below:
(a) Click on Start, Programs, Microsoft Visual Studio
.NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt.
(b) In the command window type the word cd followed by a space. Do not hit return.
(c) Keep the command window on left and bring up the
windows explorer on the right of the monitor.
(d) In the windows explorer, go to the director that
contains this file.
(e) Drag the CarComp
directory from windows explorer to the command window.
(f) Set focus on the command window and hit return.
- Study the Car.cs file in this directory. Open it with notepad or
an editor that will let you view the text file (we don’t want to use
studio in this example).
- In the command window
type the following command:
csc
/target:library Car.cs
- The assembly named
Car.dll has been created. This is
an assembly without strong name. Now type the following:
move Car.dll c:\temp\release
- Now cd to the CarUser directory:
cd ..\CarUser
- In this directory,
study the file named User.cs. Now we will
compile User.cs.
- Type the following
command:
csc
/out:prog.exe User.cs
- You should get an
error that the namespace Automobile is not found.
- Now type the following
command:
csc /out:prog.exe
/lib:c:\temp\release /reference:Car.dll User.cs
- Note that prog.exe has
been created.
- Now try the following
command:
prog
- Click No on the window
for Possible Debuggers.
- Study the error
message printed on the command window. Note how the probing happens (and
how it could not find the required assembly Car.dll).
- Try the following:
Copy c:\temp\release\Car.dll .
prog
- Note how the program
executes fine now. Now remove the Car.dll
del Car.dll
- Run the program again
and you will get the FileNotFoundException
again.
- Now type the following
command:
mkdir Car
copy c:\temp\release\Car.dll Car
prog
- Note how the program
executes fine now. Now remove the Car directory:
del Car
rmdir Car
- Let us create a
directory called bin and put the
Car.dll in it:
mkdir bin
copy c:\temp\release\Car.dll bin
prog
- You will get a FileNotFoundException again since the probing does not
look into the bin directory. Now, we will instruct the probing to look
into that directory.
- Using notepad, create
a file as follows (in command window type):
notepad prog.exe.config
- In notepad, type the
following and save and exit notepad:
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<probing
privatePath="bin"/>
</assemblyBinding>
</runtime>
</configuration>
- Now run the program
from the command window:
prog
- You will see that the
probing is picking up the assembly from the bin directory.
- Now remove the bin
directory:
del bin
rmdir bin
- Execute the prog again and you will get the FileNotFoundException
again. This time, however, note the list of directories it searched for,
includes the bin directory as well.
- The directory for probing, i.e., the private path
must be under the application’s current directory.
- Now we will see how we
can use codebase instead of probing. Using codebase, you can specify which directory to look
into.
- With code base, the
directory may be located any where you want, not just under the
application’s directory. However, there is one restriction. If the assembly
you depend on has a strong name, it can reside almost any where.
Otherwise, it has to reside under the application’s current directory. We
will play with this to see how that works.
- Modify the prog.exe.config as follows (by editing it in notepad):
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Car"
/>
<codeBase
href="file:///c:\temp\release\Car.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Now, save the prog.exe.config file and run prog
from command line.
- Notice you are getting
a FileLoadException with a message that says
Unhandled
Exception: System.IO.FileLoadException: The found private
assembly, 'Car', was located outside the application base.
- Now, lets try the following. Edit the prog.exe.config
as follows:
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Car"
/>
<codeBase
href="file:/// C:\DuraSoft_.NETWithCSharp\examplesAndLabs\session10_INFR\Examples\BuildingFromCommandLine\CarUser\dep\Car.dll"
/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Note, in the above href, there should be no space or line feed within the
string.
- Save the file in
notepad and do the following in command line:
mkdir dep
copy c:\temp\release\Car.dll dep
prog
- You will notice that
the program runs fine now. Since the Car.dll does not have a strong name,
it has to reside under the application’s directory.
- Now we will create a
strong name and see how this works.
- In the command window
type the following:
cd ..\CarSNComp
- Study the Car.cs and the AssemblyInfo.cs
in this directory. We have also provided a Strong Name file Car.snk.
- Now, compile the
program as follows:
csc /target:library
/out:Car.dll *.cs
move Car.dll c:\temp\release
- Now cd to the CarUser directory.
cd ..\CarUser
- Edit the prog.exe.config as follows:
<?xml
version="1.0"?>
<configuration>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity
name="Car" publicKeyToken="99838ddde335993d" />
<codeBase
version="0.0.0.0" href="file:///c:\temp\release\Car.dll"
/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Note this time, we are
leaving the Car.dll in the c:\temp\release directory.
- Now compile the User.cs as follows and execute:
csc /out:prog.exe
/lib:c:\temp\release /reference:Car.dll User.cs
prog
- Now, lets see one more thing. Edit the prog.exe.config
as follows:
<?xml
version="1.0"?>
<configuration>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity
name="Car" publicKeyToken="99838ddde335993d" />
<codeBase
version="0.0.0.0" href="http://localhost/Car.dll"
/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Save the file. You can
replace the word localhost to any hostname on
whose web server, the Car.dll can be placed.
- Now, do the following:
copy c:\temp\release\Car.dll
c:\Inetpub\wwwroot
- We are assuming that
your webserver’s directory is C:\Inetpub\wwwroot.
If not, use the appropriate directory.
- Now, run the program
again and see if it uses the http://localhost/Car.dll as the location for
the assembly to download from.
- If you are in the
class while trying this example, ask the instructor for the URL of Car.dll
on the instructor’s machine and try that one. You can see the difference
in output to see that a different assembly is being used.