A Simple Standalone Server for db4o as Windows Service

by Christoph Menge in db4o, Software

Download: db4oSimpleServer

Lately, I have fiddled around with Versant’s excellent dual-license object database db4o a little and I think object databases are very neat. In fact, I read a few pages in PoEAA [Patterns of Enterprise Application Architecture] today, and here and there Fowler writes how cool object databases are. The Fowler knows!

Well, anyhow when you download db4o (sign up and go for the latest release) there is no standalone server application included. That makes perfect sense, because the server needs access to your domain model / data model dll. In an object database, there are no tables, but there are objects, so you have to compile them into the application that runs the actual db4o server. There is server code which handles practically everything that is tricky, but it’s in a dll so you’ll have to write a few lines to build an application out of it. I figured this would be a great time to write my first windows service and so I mashed up some code I found on the net.

I initially thought the VS 2008 designer would set up pretty much everything, but it turned out that things are a little more complicated.

The Installer

Since a service will run in a different manner, potentially using a different user account, etc. it needs to be installed first. There is a tool called installutil.exe, but using it is a little awkward. Also, you’d have to deploy it along your application. Matt Davis posted a cool guide on stackoverflow and some additional information here on how to write a windows service that is esentially able to install itself!

Unexpected Features

One of the most peculiar features is that -should you prefer to have your own event log instead of writing to the ApplicationLog- you have to search through a tree of installers, find the EventLogInstaller and modify it instead of simply adding a new one because the ServiceInstaller will come along with the EventLogInstaller as a child:

EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
    installer.Log = ServiceConfiguration.LogName;

We also need to take care of spawning the actual worker thread, but fortunately there is a comprehensive guide on MSDN explaining this.

Using the Sample

First things first: Of course, this is not nearly production-safe code! Also, I did not include the db4o dll’s because I’m not sure about the licence terms.

The installer in 'action'. Note the Administrator prompt

  1. Download the sample, extract
  2. Download db4o, install
  3. Copy Db4objects.Db4o.dll and Db4objects.Db4o.CS.dll from the db4o install directory to the samples’ lib directory
  4. Compile
  5. Open an Administrator command prompt and find the sample’s bin folder. There is no feedback in case anything goes wrong (at best, the program crashes)
  6. Call the install routine by executing emphess.db4oServer.exe -install. This will install the db4o server and the associated event log. The server will not be started automatically.
  7. Head over to services.msc or your EventLog, where you should find a new entry!
  8. Edit the configuration file, ready to run!
  9. Don’t use this for anything production…

The App.Config

The configuration file looks like this

<?xml version="1.0" encoding="utf-8"?>
    <add key="UseLocalFile" value="false" />
    <!-- <add key="DatabaseLocalFileName" value="irrelevant"/> -->
    <add key="DatabaseHost" value="localhost" />
    <add key="DatabasePort" value="52354" />
    <add key="DatabaseServerFileName" value="C:/Development/db4oServer/db/server.db4o" />
    <add key="DatabaseUser" value="test" />
    <add key="DatabasePassword" value="test" />

The UseLocalFile setting is meant for clients so you can easily switch between file-based and network-based configurations. Since db4oServer.Shared contains a small class that holds this configuration data, you can easily reuse the code in a different app, e.g. in the client.


There are some things I’d like to add over the next weeks, so I put the list here to give me some ‘pseudo-extrinsic motivation’… (yes, I like complicated words)

  • Simple sample client
  • Out-of-band data for backup, defragmentation commands, etc.
  • Tests / Benchmarks

I hope you have any use for this.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

Related posts:

  1. My db4o Wishlist
  2. db4o Performance Pitfalls

Tags: , , , ,

← Previous

Next →


  1. [...] This post was mentioned on Twitter by .NET Links, db4o gossip. db4o gossip said: RT @dotnetlinks A Simple Standalone db4o Server as a Windows Service http://bit.ly/52gCvr: A Simple Standalone db4… http://bit.ly/6o5kGd [...]

  2. Alessandro says:

    Sounds interesting. I’m looking forward for the sample client.

  3. Good point… Will work on that soon.

  4. Emerson Rodrigues says:

    Dear Christoph,

    I try your solution, but when I start the server service, windows send me this error:
    “the db4oServer service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service”.
    Do you have any ideia of this ???


    PS: I came from Brazil. Excusime my english.

  5. Dear Emerson,

    that is a very generic error message – honestly, I think the server crashes on start. That could be because of a newer version of db4o dlls (the code is now half a year old and makes quite a bit of assumptions), or because some security issue like inaccessible file, user not authorized to open sockets, etc. Have you checked whether the config file is ok for you, i.e. whether paths are ok?

    This server is rudimentary – there is practically no error checking or logging going on, which is a very bad idea when it comes to debugging or even production use. The idea was mainly to show how to get a windows service up and running.
    You might want to add some larger `try{ }` blocks and write Exceptions to the log – that might help in finding the error.

    Sorry this isn’t too helpful, but there are too many potential errors.


  6. [...] know, this has been done before, here and here but I thought it would be nice to write one myself. Just as an exercise. I also wrote a [...]

Leave a Comment