SQLite.Net Custom Functions not getting registered after upgrading to .Net 4.5

SQLite’s ADO.NET adapter System.Data.SQLite supports for user defined SQL functions. New functions are implemented by deriving a class from SQLiteFunction class and applying SQLiteFunctionAttribute to it. At run time SQLite.Net reflects on all the assemblies loaded in the AppDoamin to automatically register all the use-defined SQL functions.

We have a few user-defined functions which we have been using for a long time without any problem. But, after installing .Net 4.5 on my system none of the user-defined function work. Somehow SQLite was not able to find the custom functions after the 4.5 upgrade. It turned out that in .Net 4.5 CLR Microsoft tweaked the order in which static constructors get called.

Here is the SQLiteFunction static constructor where all the user defined functions are registered.image

In order to minimize the number of assemblies that need to be examined, the code only goes through types which reference System.Data.SQLite.dll. That’s a good idea, but, it does it in a weird way, it compares the referenced assembly name with the name of the assembly that called this method (GetCallingAssembly) , rather than the name of the assembly that contains this code (GetExecutingAssembly).

This never caused any problem in .Net 4.0 and older version because SQLiteFunction’s static constructors always got called from within its instance constructor.


But, in .Net 4.5, CLR calls the static constructor before the instance constructor.


And in this case the calling assembly is no longer the SQLite assembly itself, and therefore the SQLiteFunction’s static constructors skips all the user-defined functions.

Interestingly I was only able to reproduce the problem in an asp.net web application, somehow in console app static constructor gets called from instance constructor even in .Net 4.5.

The sample app I used to test this is available in GitHub here.

Fixing the problem should be easy enough, either replace GetCallingAssembly with GetExecutingAssembly and recompile the SQLite.dll or just manually register all the user defined functions using SQLiteFunction’s static RegisterFunction method.

UPDATE: SQLite.Net maintainers were quick to address this, mistachkin fixed it on the same day. Here is the url for sqlite.net ticket: http://system.data.sqlite.org/index.html/tktview?name=4e49a58c4c. Thanks mistachkin.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s