Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Use Assembly/Strong Named DLL in C# /SSIS script without install in GAC.

This post will describe the use of Assembly type DLL without installing it on GAC

Always we have to install an Assembly first then we can use the DLL file into C# code. But when we need to develop something portable and need to run from any machine. So, for this case, we have to use an assembly like a normal DLL, means which DLL has no any strong name. But we everybody knows that assembly type DLL has a strong name so that we can’t use it without install.
Furthermore, there are many procedures to use an Assembly or Strong Named DLL as a reference in the C# coding …
·         Remove Strong Name from the DLL
·         Use DLL merger to change the strong name by the new one, every time we need to run the C# code.
But above process is not so easy to Implement. But there is another procedure available which is as simple as it says.
·         Load the Assembly DLL file at the runtime of the beginning of the program which needs it to execute.
Yes, it’s so easy to implement and every time c#.NET will load the assembly from the defined location. Follow below points to implement the procedure.
·         Because of we have to load the assembly/DLL at the beginning of the program, we will create a static constructor of the class of first entry point, that means Main Class should have a static constructor.
·         Put the assembly/DLL file on a fix location and mention this location here to load at runtime.
·         And then implement an interface/event handler named ResolveEventHandler to include the missing assemblies from the pre-defined location
Let’s see the coding for that ...

NOTE: you need to change the GREEN parts only

public class assemblyLoadAtRuntime
{
…..
…..
…..

static string[] Path = { @"D:\temp\" , "AssemblyDLL" } ; // { "Path of the DLL file" , "Name of the DLL file without extension" }

     static assemblyLoadAtRuntime ()
     {
         AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentResolve);
     }

     static System.Reflection.Assembly CurrentResolve (object sender, EventArgs args)
     {
         String MyAssembly = Path[0] + Path[1];
         byte[] assembly = File.ReadAllBytes(MyAssembly);

         if (args.Name.Contains(Path[1]))
         {
System.Reflection.Assembly.Load(assembly);
         }
         return null;
     }

…..
…..
…..

}

Here assemblyLoadAtRuntime is the main class
AssemblyResolve represent the list of assemblies
And finally, assemblyLoadAtRuntime is the interface to implement to load the SN DLL.

This procedure is very much simple but it can solve the problem of assembly use.



This post first appeared on Nothing Is Bug Free, please read the originial post: here

Share the post

Use Assembly/Strong Named DLL in C# /SSIS script without install in GAC.

×

Subscribe to Nothing Is Bug Free

Get updates delivered right to your inbox!

Thank you for your subscription

×