Showing posts with label Strong Name Key. Show all posts
Showing posts with label Strong Name Key. Show all posts

Sunday, March 9, 2014

Implementation of Shared Assemblies


Implementation of Shared Assemblies

Create a Class Library Project.

  • Create a new Class Library project.
  • Write the required code in the project.

Create a strong name key.

  • Right click on the project in the Solution Explorer and choose Properties.
  • In the project properties, select the check box Sign the assembly.
  • In the Choose a strong name key file‖ drop down, select <New> option.
  • In the Create strong name key‖ dialog box, enter the name of the strong name key file.
  • If password security is not required, uncheck the Protect my key file with a password checkbox.
  • Click on OK.

Customize the “Assembly Information” (AssemblyInfo.cs).

  • This is optional step.
  • To change the additional details of the assembly like displayed name, version, company, copy right, description etc., open “AssemblyInfo.cs” file from the Properties folder in the Solution Explorer.

Generate the DLL File.

  • Build the class library project.
  • Then the .dll‖ file will be generated in the bin\debug folder of your class library project.

Write the assembly into GAC (Global Assembly Cache).

  • Open the following folder.
  • C:\Windows\Assembly
  • Drag and drop the ―.DLL file‖ from ―bin\Debug‖ folder into the
  • c:\windows\assembly folder.
  • After dragging, the name of your shared assembly will appear in the existing list.
  • Now, the shared assembly is ready. The rest of your work involved with the usage of the shared assembly.
Invoke the Shared Assembly.
  • Create the executable project (Console application / windows application).
  • Click on Project – Add Reference.
  • Click on Browse tab.
  • Open the class library project‘s bin\Debug folder.
  • Select the dll file and click on OK.
  • Then the reference of the selected shared assembly will be added to the current project.
  • Then you can construct objects for the required class in the class library and perform required activities on that.

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();
  }
 }
}


Flag Counter