Jump to content

Tutorial


DooVoo

Recommended Posts

Hello All,As a C++ developer I was used to being able to write all my classes in seperate files and referencing them using the #include directive. When I moved to C# development I couldn't work out how to do this and so started researching. What I found were "Assemblies", more specifically "Class Libraries". After many hours of hair pulling and furstrated rantings at tutorials that didn't explain the process in a nice simple step-by-step approach, I managed to get an implementation working. To help cement my knowledge I decided to write this following tutorial and post it on here to help people looking for the same information and for those with more knowledge to correct any mistakes or misconceptions I have made. So here goes...IntroductionASP.NET makes use of several types of assembly. This tutorial aims to describe how to manually create a Single-File Class Library and make it globally available to your applications. For this you will need:Microsoft Internet Information Services v5.1 (Available with WindowXP Professional)Microsoft .NET Framework v1.1 Microsoft .NET SDK v1.1Notepad (or your favorite text editor)What is a Class Library?A Class Library is a compiled DLL file that contains one or more class definitions. They can then be referenced by other source files in your applications for reusability.To make these classes globally available they must be registered in the Global Assembly Cache (GAC) as well as the machine level configuration. Below are all the steps needed to achieve this.Step1: Create a Strong Name key pairBefore an assembly can be added to the GAC it must have be Strong-Named. This has many benefits, which are detailed here. To make an assembly Strong-Named, we must first create a Strong Name key pair. This is done using the SDK tool “sn.exe”.At the command prompt navigate to your code directory and type:

sn -k KeyPair.snk

This will generate the file “KeyPair.snk” that holds the private and public keys that you will use with your assembly.Note: It should be pointed out that for the “sn” command to work in your application directories you will need to have run the “sdkvars.bat” file in the “C:\Program Files\Microsoft.NET\SDK\v1.1\Bin” folder in the same Command Prompt session!Step 2: Add assembly attributes to the source files.Now we have the key pair file, we need to tell the compiler where it is located. This can be done a number of ways, but we’ll do it in the source code for your Class Library. First, we need to use the namespace that deals with this. So add the following “Using” statement:Using System.Reflection;Following your “Using” statements add the following attribute:

[assembly:AssemblyKeyFileAttribute(@"KeyPair.snk")]

This, obviously, sets the path to your “KeyPair.snk” file. This attribute is one of many available for providing information about your assembly. A complete list of these attributes can be found here.Step 3: Compile the source file into a LibraryNow the source file has been setup and associated with a Strong Name key pair we can compile it into a DLL. This is done using the SDK compiler tool “csc.exe”.At the command prompt navigate to your code directory and type:

csc /out:MyCode.dll /target:library MyCode.cs

This will compile your source into a DLL with all the Strong Name assembly information you provided.Step 4: Add the assembly to the Global Assembly Cache [GAC]Including the assembly into the framework is simple. Just do the following:1. Select: Start -> Program Files -> Administrative -> Microsoft .NET Framework 1.1 Configuration2. Select: Assembly Cache3. Select: Action -> Add4. Browse to your compiled DLL and open itStep 5: Add the assembly to the machine.configNow, the final step in setting up your assembly, updating the machine level configuration machine.config file. The machine.config file is located by default in: “C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG”Once open, scroll down to the “<add assembly=…” lines and add a new one at the end. This will look something like below:

<add assembly="MyCode, Version=1.0.1.0, Culture=neutral, PublicKeyToken=0249f9723h97"/>

Note: The PublicKeyToken value is the hexadecimal version of the Strong Name public key you created earlier. You can find out this value by running the following command from your command prompt:

sn -Tp MyCode.dll

Step 6: Using your new Code LibraryNow that you have created and configured your new code library all that is left is to use it!Do this by creating a new source file and referencing the namespace you used in your source file, such as:

Using MyNameSpace;

With this any classes that you created will automatically be available for instantiation!This concludes the tutorial. If you have any suggestions or comments, please let me know in this thread.

Edited by DooVoo
Link to comment
Share on other sites

Was this useful for anyone? I was thinking of writing something similar each time I learnt something (I think is) useful, but if it's not benefitting anyone here then there isn't much point. Please let me know :)

Link to comment
Share on other sites

  • 3 weeks later...

While reading this I flipped my head sideways(like 45 degrees or so :) ) trying to figure out what is this about... so... what is it about? If you want to acces any kind an information of an ASP.NET file, isn't there some kind of server side include? The ASP(.NET) tutorials on w3schools seem pretty complete. How come this isn't covered?

Link to comment
Share on other sites

in .Net you can easily open a given file and read it contents and dump it into a page but a better approach is to compile a custom class that contains your content that you want to reuse. then with one line you can call the class and it sumps in hte content for you...sort of like SSI but it conseals all your source code.in the .aspx you could also insert an include just the same as in ASP

Link to comment
Share on other sites

aspnetguy is correct, but sometimes you may want your code to be more portable or accessable. By turning your classes into a class library you can access them by just referring to their namespace with a "using" statement. I've found this approach to be very flexible when working with other developers on the same project.Edit: It's far too early for spelling and grammer. Please excuse the poor attempt at English above :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...