Sunday, March 9, 2014

Shared Assemblies


Shared Assembly

CREATE A CLASS LIBRARY PROJECT.

  1. Open Visual Studio 2008.
  2. Click on ― > File‖ ―> New ―> Project.
  3. Select the language as ―> Visual C# and project template as ―> Class Library.
  4. Enter the name as ―> SharedAssemblyLibrary.
  5. Click on OK. It creates a new class library project. Initially it contains a new class called class1.
  6. In the solution explorer, rename the class1.cs as "MyLibraryClass.cs”.
  7. Then type the following code.
PROGRAM FOR CLASS LIBRARY PROJECT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharedAssemblyLibrary
{
  public class MyLibraryClass
 {
  public bool IsValidLogin(string Username, string Password)\
  {
   if (Username == "system" && Password == "manager")
   return true;
  else
   return false;
  }
 }
} 
CREATE A STRONG NAME KEY.

  1. Right click on the project in the Solution Explorer and choose Properties.
  2. In the project properties, select the check box Sign the assembly
  3. In the Choose a strong name key file drop down, select option.
  4. In the Create strong name key‖ dialog box, enter the name of the strong name key file as “MyKeyFile”.
  5. Uncheck the ―Protect my key file with a password‖ checkbox.
  6. Click on OK.
CUSTOMIZE THE “ASSEMBLY INFORMATION” (ASSEMBLYINFO.CS).

Change the assembly version as “1.5.0.0”.

GENERATE THE DLL FILE.

Build the class library project by clicking on ―Build‖ menu – ―Build Solution‖.

WRITE THE ASSEMBLY INTO GAC (GLOBAL ASSEMBLY CACHE).
  • Open the following folder
  • C:\Windows\Assembly
  • Drag and drop the “SharedAssemblyLibrary.DLL‖ file from ―bin\Debug‖ folder into the c:\windows\assembly folder.
INVOKE THE SHARED ASSEMBLY.


  • Create a new Console Application. Name: SharedAssemblyDemo
  • Click on Project ― Add Reference.
  • Click on Browse tab
  • From the class library project‘s bin\Debug‖ folder, select the “SharedAssemblyLibrary.dll file and click on OK. Then the .dll file reference will be added
PROGRAM FOR SHARED ASSEMBLIES
                                                      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedAssemblyLibrary;
namespace SharedAssemblyDemo
{
class Program
 {
  static void Main(string[] args)
  {
   MyLibraryClass mlc = new MyLibraryClass();
   string Username, Password;
   Console.Write("Enter Username: ");
   Username = Console.ReadLine();
   Console.Write("Enter Password: ");
   Password = Console.ReadLine();
   bool result = mlc.IsValidLogin(Username, Password);
   if (result == true)
   Console.WriteLine("\nLogin successful!");
  else
   Console.WriteLine("\nInvalid Username / Password!");
   Console.Read();
  }
 }
}