tags:

views:

690

answers:

5

I created a console application using C# that references external DLLs. When I run it on my dev machine, everything works fine. On the production machine, I get a "type initiatization" error. Looking into this, it seems it may because the app can't find the referenced DLLs.

On my dev box, the referenced DLLs are in the GAC, but not on the production one. When removing the DLLs from the GAC on the dev box, the same error occurs (unless I run it from a local Visual Studio build in debug mode).

I'm more familiar with web site setups, and know there that the DLLs can be placed in the bin directory or the GAC so they can be found by the web application. But I'm not sure about how this works for console apps.

I'm reluctant to put the DLL into the GAC on the production box, as it's only needed for this one small application. Are there other ways for me to deploy the console app and have it find its required assemblies?

+7  A: 

The simplest answer is to put the DLL's in the same directory as the Console Application and it will find them.

The longer answer is fairly complex as there are many factors which influence how and where the CLR looks for referenced assemblies. I encourage you to take a look at the following article from MSDN which goes into great detail as to how this works.

JaredPar
A: 

When you examine the exception you get (and you may need to dive into the inner exception), you should see a log of all the locations searched (the "fusion log").

I'd recommend just placing the dependent DLLs in the same directory as your console app.

Michael Petrotta
Actually, he just needs to display `ex.ToString()`.
John Saunders
+1  A: 

The GAC (Global Assembly Cache) registers your .dll's so you don't have to have them in the working directory of your application. All of .NET's .dll's (System.IO.dll, System.dll, etc) are registered through the GAC which means you don't have to have them in your application's directory. By default, if a .dll is not registered in the GAC, then the program will look in it's own directory for the missing .dll.

So, you have two choices:

  1. Add your referenced .dll's to the GAC, or
  2. Add your referenced .dll's to the working directory of your application
  3. Go to your references on the solution/project and select properties for that reference, set "CopyLocal = true" (Credit to Partha above for this)
Zack
+1  A: 

- Right click on the assembly name in your project reference.
- select Properties
- In the properties window set CopyLocal to true

Partha Choudhury
Oh, forgot to mention that in my answer. :P +1
Zack
A: 

You can deploy referenced dlls where you want. You have to add an App.config (add/new item/application configuration file) to your base project and use the tag probing (configuration/runtime/assemblybinding/probing) to indicate a path for your dlls.

You have to copy the dll or dlls to that path and add a reference to it in your project. In reference properties put "copy local" to "false".

Marco