How to detect missing .NET link at runtime?

My application contains links to an external library (SQL Server management objects). Apparently, if the library is not in the runtime system, the application still works until methods that use classes from this library are called.

Question 1: Is this a given behavior or just a (successful) side effect of how the CLR loads libraries?

To determine if this link is available, I am currently using the following code:

Function IsLibraryAvailable() As Boolean Try TestMethod() Catch ex As FileNotFoundException Return False End Try Return True End Function Sub TestMethod() Dim srv As New Smo.Server() ' Try to create an object in the library End Sub 

It works, but it seems pretty ugly. Note that it only works if TestMethod is a separate method, otherwise an exception will be thrown at the beginning of IsLibraryAvailable (before try-catch, even if an object instance occurs in the try-catch block).

Question 2: Is there a better alternative?

In particular, I'm afraid that optimizations like the inlining function might stop my code.

+6
source share
1 answer

This is expected since JIT is lazy at the level of each method. Note that nesting is not a problem here, as it also applies to JIT, not the compiler.

The best options:

  • make sure that the application is installed with everything that it needs.
  • using ilmerge or similar to create a single assembly (if possible)

Personally, I just use the first option.

+4
source

Source: https://habr.com/ru/post/893700/


All Articles