Including SQLite.Interop.dll into your C# project

When developing C# applications I love to be able to distribute a single executable without having to install the software or worry about dependencies.

Whenever I create a new C# project which is going to refer to external libraries such as the wast popular Html Agility Pack, I normally start by adding Costura to my project using NuGet.

If you haven’t used NuGet before I highly recommend you to do so! It’s just awesome and makes handling decencies much easier in terms of keeping track of them and dependencies, updating and building. Normally it’s enough to just add Costura to your project and all of the external libraries will automatically be embedded into your final executable upon build.

However, when I started to work with SQLite.Interop.dll I wasn’t able to include the library. This guide will show you what I had to do in order to solve this issue. This will most likely work for all other libraries that are targeted to a specific platform (32- and 64-bits).

Create a new project

Start by creating a new C# project. For this tutorial I’m creating a new WPF Application using .Net Framework 4.5.

NuGetIf you don’t have the Package Manager Console open, go to View->Other Windows->Package Manager Console in order to open it.

In order to install Costura and SQLite run the following two commands (one after the other):

Once you build the project you’ll notice that SQLite creates a x86 and x64 directory in /bin/Debug and a whole bunch of other DLL files. You should also notice that your WpfApplication.exe is about 1894 KB in size. Just as a reference before and after embedding SQLite.

Embedding SQLite.Interop.dll

Now comes the fun part. Let’s get rid of all the external dependencies! After installing Costura a files called FodyWeavers.xml was added to your project. Normally you don’t have to specify what DLL files to embed, but this time we do.

Make sure your xml file looks like this:

CosturaNow we have specified that we want to embed the DDL-files, but we also need to include them into our project. Next create to new folders called costura32 and costura64 and make sure to copy the correct version of SQLite.Interop.dll into each of them. Put simply, copy the dll-file from x86 to costura32 and x64 to costura64. You will also have to change the Build Action for both files to Embedded Resource.

Once done rebuild your project and notice that your executable now is quite a bit larger then it was before. Now it’s safe to delete or move your executable outside the debug directory without breaking any dependencies.

Optional: Clean output folder automatically

Costura only merges dependencies. It does not handle cleaning those dependencies from your output directory. If you want to clean this directory automatically after each build you can install the following cmdlet:

 

Share this:

13 comments, add yours.

Mike Mankus

This information was incredibly helpful and solved my SQLite/Costura issues. Thank you for the informative post!

John

This was helpfull for me since my sqlite database wasn`t embedded on my application and I had to do that.

Alfio Budel

After hours of searching i finally landed in the right place…Thank you for the solution…This one gave me quite the headache

Benjamin Krause

Benjamin Krause

Author

You’re welcome 🙂

Cameron V

Hello,

I’ve followed the instructions, and the size of my executable certainly went up after embedding the sqlite.interop.dll. However my application still gives the error “Unable to load DLL ‘SQLite.Interop.dll’. I’ve gone over the instructions a few times but I am not sure where there is any error. Any suggestions?

Benjamin Krause

Benjamin Krause

Author

Hi, I’m not sure, but I’ve created a demo solution for you, hope that helps you out:
https://github.com/IDmedia/demo.SQLiteCostura

Danie

Thanks after trying everything for days, I finally found your solution, the only thing that worked for me.

Oleg Zarevennyi

Thank you very much for simple and step-by-step tutorial with explanations, it saves me so much time! Fody is really nice

Jamil

I can’t find sqlite.interop anywhere.

AKCoder

that was exactly what i wanted, thank you so much

Mehmet

Benjamin Krause

Benjamin Krause

Author

Thank you. 🙂

Leandro

Is it possible to do this in windows forms?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.