tags:

views:

75

answers:

4

Something i've never really done before, but what is the best way to make sure that any external assemblies/dll's that my application uses are available, and possibly the correct version.

I wrote an app that relies on the System.Data.SQLite.dll, i went to test it on a machine where that dll was missing, and my app just threw up a runtime exception because the dll was missing. How can i trap this error?

A: 

You should trap the error outside of your main loop.
Or if you want to ship/locate your own assemblies you can try overriding the assembly probing: Link.

Nescio
+1  A: 

What you want to do is use reflection to check and see if the assembly can be loaded into memory. Wrap that up in a try..catch block and handle any of the specific exceptions that come from it.

Try
    Assembly.Load("System.Data.SQLite, Version=1.0.22.0, Culture=neutral, PublicKeyToken=DB937BC2D44FF139");
Catch ex As FileNotFoundException
    //do something here
End Try
Josh
A: 

You could use a setup project to build an installer for your app. This would analyze all of the static dependencies and produce an installer that ensures that the target machine gets everything it needs.

Wayne
+1  A: 

(I've set the community-owned flag on this one, because this is mostly all from my gut instinct, and I've probably missed a crucial step in there somewhere)

Short answer: It's generally a good idea to deploy your dependencies along-side your application, using an installer. Without them, as you've noticed, there is very little chance of your application working.

Long answer: Ok, say you have extra functionality you want to provide if something else is installed on the target machine. Here's some general guidelines to do it:

  1. For any type that has a field, property, event, parameter, or return-value that references a type defined in the possibly uninstalled assembly: must be wrapped with an interface, and replace all other field, parameter, return-value, or local variable declarations to use the interface.
  2. Any time you go to construct one of the previously wrapped classes, you must use the System.Activator.CreateInstance method, and wrap it in a try/catch filtering on 7 different exception types:

    • FileNotFoundException
    • FileLoadException
    • BadImageFormatException
    • TypeLoadException
    • MissingMethodException
    • MissingMemberException
    • MissingFieldException

    If one of those is caught, you must either provide an alternative implementation of the previously created interface, or write your code so that it checks for null any time it references that object.

Alex Lyman
Thanks for that explanation, ill use the example code provided by Josh, but this info is very helpfull as well
beakersoft