creating sample component in assemblies?

Step 1: Creating your sample component

Here is the code for the component. It just includes one method that returns a string.

1. using System;

2. namespace BAJComponents

3. {

4.    public class Sample

5.    {

6.       public string GetData()

7.       {

8.          return "hello world";

9.       }

10.   }

11.}

Step 2: Generate a key file

To generate the key file, issue the following command at the command prompt.

1. sn -k sample.key

This will generate the key file in the same folder.

Step 3: Sign your component with the key

Now, you will sign the assembly with the key file you just created.

1. csc sampleGAC.cs /t:library  /a.keyfile:sample.key

Step 4: Host the signed assembly in the Global Assembly Cache

You will use the AL utility to place the assembly in the Global Assembly Cache:

1. AL /i:sampleGAC.dll

After hosting, the assembly just goes to the WINNT\Assembly folder and you will find your assembly listed there. Note how the assembly folder is treated differently than normal folders.

Step 5: Test that your assembly works

Now, create a sample client application that uses your shared assembly. Just create a sample code as listed below:

1. using System;

2. using BAJComponents;

3. public class SampleTest

4. {

5.    static void main()

6.    {

7.       sample x= new sample();

8.       string s= x.getdata();

9.       console.writeline(s);

10.   }

11.}

Compile the above code by using:

1. csc sampletest.cs /t:exe /r:<assembly_dll_path_here>

Now, copy the resulting EXE in any other folder and run it. It will display "Hello World", indicating that it is using your shared assembly.


No comments:

Post a Comment