Many applications consist of an EXE that depends on many DLL files. When deploying an application all files must be deployed. So carrying all dependent DLL files for an exe during deployment is a problem and you may miss one of the dependent DLL file. To overcome on this problem you can create a single deployable EXE in Visual Studio. In visual studio, we can make configuration of EXE in such a way that, we can load an assembly at runtime that is located in a folder that is not the bin folder of the application. You do not have to put an assembly that an application must use at runtime in the bin folder of the application. You can put the assembly in any folder on the system, and then you can refer to the assembly at runtime. Following procedure explain the same –
The technique uses Appdomain’s “AssemblyResolve” event to achieve it. First, identify all the DLL files that your EXE file depends on. Then add these DLL files in one of the folder in your project. For example, I am using following DLL files in my application which I have added in dll folder in my application –
Add the reference of these DLL files in the application from the folder. In my case I added reference from “dll” folder. Then in the “dll” folder, select each DLL file and open properties; change “Build Action” to “Embedded Resource”. This causes C# compiler to embed the DLL files into your EXE file and then you can deploy this one EXE file. At runtime, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. The “AssemblyResolve” event fires whenever CLR tries to bind an assembly and fails. The “AssemblyResolve” event handler must return an [Assembly] object.
Also the call to event handler method registration must be the first statement and rest of the logic should be wrapped in another method. In my case, for example, I am generating bitmap image in Main method of my console application. Therefore my code segment in Main method is as follows –
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
GenerateImage();//this can be any method containg other work you need to carry out
}
The code for Assembly Resolve is as follows –
using System.Reflection;
/// <summary>
/// Event is raised when CLR is not able to find referenced assemblies (in this case - gheat.dll, GMap.NET.Core.dll, Microsoft.WindowsAzure.storageClient.dll)
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns> Assembly to be loaded from embeded resource.</returns>
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly objExecutingAssemblies;
Byte[] assemblyData = null;
objExecutingAssemblies = Assembly.GetExecutingAssembly();
AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
//Build the path of the assembly from where it has to be loaded.
//Console.WriteLine(args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll");
//Console.WriteLine("GHeatReducer." + new AssemblyName(args.Name).Name + ".dll");
//GHeatReducer.dll. - this name is retrievd from ILDasm tool and by opening GHeatReducer.exe in the ILDasm tool. In ILDASM tool select name rpesent against “.mresource” to write in GetManifestResourceStream method. “GHeatReducer” is the name of namespace and “dll” is the name of folder where DLL files to be referenced is present as shown in above screenshot.
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("GHeatReducer.dll." + new AssemblyName(args.Name).Name + ".dll");
assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
break;
}
}
//Return the loaded assembly.
return Assembly.Load(assemblyData);
}
public static void GenerateImage()
{
//code to generate image goes here
}
Hope this helps.
Cheers…
Finally found what I was looking for :) Thank you very much
ReplyDeleteI was trying to use the method here:
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx?PageIndex=1#comments
The event handler method is slightly cleaner but I was missing one important point.
I was adding the event handler in the Main() method and executing code in the same.
Just wrapping the rest of the code in another method solved this for console app.
Thanks again.
Hi,
ReplyDeleteSame thanks as David ;)
welcome David..happy to help you...
ReplyDeleteI'm having a problem with this.. this is a small console application that solves a ProjectEuler problem. It requires the FSharp.PowerPack DLL.
ReplyDeleteI've done exactly as this article suggests. Add the fsharp.powerpack.dll file to the solution, changed the build action to "embedded resource". Added a reference to the dll file "..\ConsoleApplication1\dlls\FSharp.PowerPack.dll"
Here's what my "Main" function looks like:
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
DoWork();
}
Yet, when I copy the ConsoleApplication1.exe from the debug folder (after doing a build), to a computer that doesn't have F# library installed, I get "Unhandled exception: system.io.fileloadexception: could not load file or assembly 'FSharp.PowerPack...." when running it...
Hey Wes,
DeleteAssuming, you are using F# code only to load F# dll; as I don't know much about F#, Make sure that you have given correct path from .mresource in the code. Also make sure that, when you build your EXE, does it have size including the size of your DLL?
I ended up using the code at the bottom of this forum post:
Deletehttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral
/thread/13868df7-897f-48f1-81cd-c41b925ac928/
and it worked like a charm.
I'm still not sure what I needed to do differently with the code you provided. :(
Maybe you could d/l the dll and try it?
http://dl.dropbox.com/u/1586533/FSharp.PowerPack.dll
do something like this in your code:
using Microsoft.FSharp.Math;
BigRational someNumber;
someNumber = 123456789123456789123456789;
How do you do this in Visual Studio 2005. There is no program.cs in the class library project. Where do I write this code.
ReplyDeleteI have an exe that depends on DLL_1. DLL_1 depends on several other dlls. I have added all the dlls as resources (embedded resource) in DLL_1. If I am correct the above code should go into DLL_1.
Thanks for the help,
Rao
Hey Rao, in class library you will not find program.cs as this is for console application. For your scenario, the above code of main method and handler method will go in EXE and not in DLL_1. hope this helps.
DeleteHello,
ReplyDeleteI am getting the following error when I use the code in Exe.
Could not load file or assembly 'ServiceAgent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependenciesAn expected resource in the assembly manifest was missing. (Exception from HRESULT: 0x80131532)
Thanks,
Rao.
It might be the case that, Service Agent dll is also required to be added in the folder. Also make sure that, you are referring the correct path. Use ILDASM tool to reach to the manifest of serviceagent dll. When you build the EXE, does it have size including the size of serviceagent dll. If not then service agent is not getting added in the EXE. So add and then try. Hope this helps.
ReplyDeleteYes. Altogether I added 10 other dlls in DLL_1. And it increased the size after I added each dll.
ReplyDeleteWhat code should go in GenerateImage()
public static void GenerateImage()
{
//code to generate image goes here
}
Thanks
Rao
No code will go generate image method in your case. It is just a place holder method. This method can be any business logic of your choice.
ReplyDeleteNot sure of problem you are facing.
Please tell me how to access the functions that are inside the emebedded dll?
ReplyDeleteVery cool tutorial,
ReplyDeleteI have tested the code, and had the initialization inside the static ctor of the console app.
This eliminates the need to have additional function in Main() if you don't want it.
Here is the code:
class Program
{
static Program()
{
ResolveAssemblies();
}
private static void ResolveAssemblies()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = "Fun.Resources." +
new AssemblyName(args.Name).Name + ".dll";
using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
}
static void Main(string[] args)
{
if (null == args || args.Length == 0)
{
Logger.Info("No command line arguments found!");
return;
}
for (int i = 0; i < args.Length; i++)
{
Logger.Debug("arg at [{0}]: {1}", i, args[i]);
}
}
The embeded assembly is just a simple Logger.
Great - this was just what I have been looking for! Thank you very much!
ReplyDeletewould someone please post a complete application? With an XML resource?
ReplyDelete