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 () // static Constractor of your class
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentResolve);
}
static System.Reflection.Assembly CurrentResolve (object sender, ResolveEventArgs args)
{
if (args.Name.Contains(Path[1])) // change this code if you have many dll file to load
{
return System.Reflection.Assembly.LoadFile(Path[0] + Path[1]+".dll");
}
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.