views:

68

answers:

3

We have a .net 2008 application which uses two dll's to remote to a server to get information. the returned object. One of the properties of this object is an enum which is held in a third dll.

For some reason when we build the third dll is not being added, despite the fact i have now included all three dll's in the solution.

at runtime when the code deserialises the object we get a dll not found exception specifying that the dll is missing, for the dll in question, it contains only enums (not my design)

Any idea how i can force the dll to be added?

I can just add the dll as part of the harness or service but I shouldn't have to as it should be picked up as a dependency of the main project

A: 

Do you have the "Copy Local" flag set to true in the reference properites?

Simon
Yes it is selected
Kev Hunter
+1  A: 

use post-build event command to add the dll into your bin directory.

HotTester
that's a bit more hacky than i would like
Kev Hunter
Its not hacky! This feature was given by microsoft to do any specific work that cannot be handled by .net itself.
HotTester
In what way can .net not usually work out which dll's are needed for an app to work? The solution has a dependency on the dll, therefore it should be automatically copied into the debug or release folder.
Kev Hunter
According to your question u r consuming 2 dll which in turn use in a third dll. I am assuming that these are unmanaged. This means that the first 2 dll are being directly being used but the third is not. Hence .net cannot get the dependency of the 2 dlls being used. The only way around it is to use the post-build event command
HotTester
they are all managed code
Kev Hunter
+3  A: 

That's because you don't need the DLL. An enum assignment gets compiled to its numeric value, there is no reference the type in the IL. For example, this:

public enum Numbers { Zero, One, Two, Three }

static void Main(string[] args) {
  var n = Numbers.One;
}

gets compiled to this:

  IL_0001:  ldc.i4.1          ; NOTE: constant 1 being used here
  IL_0002:  stloc.0

The compiler automatically filters out "using" directives for assemblies that are not actually used.


Apparently you've found a wrinkle in this, it is very important that you put details like this in your question. Yes, binary serialization will put a type reference in the serialized data, something the compiler cannot see. The only workaround for this is to ensure the assembly gets copied into the build directory. Project + Add Existing Item, navigate to the DLL. Select it in the Solution Explorer window, set the Copy to Output Directory property to "Copy if Newer".

Hans Passant
Good to learn that..
waheed
when the code deserialises the object we get a dll not found exception specifying that the dll is missing, for the dll in question, it contains only enums (not my design)
Kev Hunter
@Kev - I updated my answer, below the line.
Hans Passant