Building From Command Line

 

  1. 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.
  2. First create a directory named temp on your c:\ if one does not exist already.
  3. Then create a directory named release under c:\temp.
  4. 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.

  1. 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).
  2. In the command window type the following command:

csc /target:library Car.cs

  1. 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

  1. Now cd to the CarUser directory:

cd ..\CarUser

  1. In this directory, study the file named User.cs. Now we will compile User.cs.
  2. Type the following command:

csc /out:prog.exe User.cs

  1. You should get an error that the namespace Automobile is not found.
  2. Now type the following command:

csc /out:prog.exe /lib:c:\temp\release /reference:Car.dll User.cs

  1. Note that prog.exe has been created.
  2. Now try the following command:

prog

  1. Click No on the window for Possible Debuggers.
  2. 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).
  3. Try the following:

Copy c:\temp\release\Car.dll .

prog

  1. Note how the program executes fine now. Now remove the Car.dll

del Car.dll

  1. Run the program again and you will get the FileNotFoundException again.
  2. Now type the following command:

mkdir Car

copy c:\temp\release\Car.dll Car

prog

  1. Note how the program executes fine now. Now remove the Car directory:

del Car

rmdir Car

  1. Let us create a directory called bin and  put the Car.dll in it:

mkdir bin

copy c:\temp\release\Car.dll bin

prog

  1. 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.
  2. Using notepad, create a file as follows (in command window type):

notepad prog.exe.config

  1. 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>

  1. Now run the program from the command window:

prog

  1. You will see that the probing is picking up the assembly from the bin directory.
  2. Now remove the bin directory:

del bin

rmdir bin

  1. 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.
  2. The directory for probing, i.e., the private path must be under the application’s current directory.
  3. Now we will see how we can use codebase instead of probing. Using codebase, you can specify which directory to look into.
  4. 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.
  5. 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>

  1. Now, save the prog.exe.config file and run prog from command line.
  2. 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.

  1. 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>

  1. Note, in the above href, there should be no space or line feed within the string.
  2. Save the file in notepad and do the following in command line:

mkdir dep

copy c:\temp\release\Car.dll dep

prog

  1. 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.
  2. Now we will create a strong name and see how this works.
  3. In the command window type the following:

cd ..\CarSNComp

  1. Study the Car.cs and the AssemblyInfo.cs in this directory. We have also provided a Strong Name file Car.snk.
  2. Now, compile the program as follows:

csc /target:library /out:Car.dll *.cs

move Car.dll c:\temp\release

  1. Now cd to the CarUser directory.

cd ..\CarUser

  1. 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>

  1. Note this time, we are leaving the Car.dll in the c:\temp\release directory.
  2. Now compile the User.cs as follows and execute:

csc /out:prog.exe /lib:c:\temp\release /reference:Car.dll User.cs

prog

  1. 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>

  1. Save the file. You can replace the word localhost to any hostname on whose web server, the Car.dll can be placed.
  2. Now, do the following:

copy c:\temp\release\Car.dll c:\Inetpub\wwwroot

  1. We are assuming that your webserver’s directory is C:\Inetpub\wwwroot. If not, use the appropriate directory.
  2. Now, run the program again and see if it uses the http://localhost/Car.dll as the location for the assembly to download from.
  3. 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.