<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>emphess .NET &#187; ServiceBase</title>
	<atom:link href="http://www.emphess.net/tag/servicebase/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emphess.net</link>
	<description>Freshly Draught Code</description>
	<lastBuildDate>Fri, 11 Nov 2011 11:57:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Simple Standalone Server for db4o as Windows Service</title>
		<link>http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/</link>
		<comments>http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 03:48:38 +0000</pubDate>
		<dc:creator>Christoph Menge</dc:creator>
				<category><![CDATA[db4o]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[EventLog]]></category>
		<category><![CDATA[Object Database]]></category>
		<category><![CDATA[OODBMS]]></category>
		<category><![CDATA[ServiceBase]]></category>
		<category><![CDATA[ServiceInstaller]]></category>

		<guid isPermaLink="false">http://www.emphess.net/?p=73</guid>
		<description><![CDATA[Download: db4oSimpleServer Lately, I have fiddled around with Versant&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>Download: <a href='http://www.emphess.net/wp-content/uploads/2010/01/db4oSimpleServer.zip'>db4oSimpleServer</a><br />
<a href="http://www.emphess.net/wp-content/uploads/2010/01/solution.png"><img src="http://www.emphess.net/wp-content/uploads/2010/01/solution.png" alt="" title="Solution Layout" width="222" height="372" class="alignright size-full wp-image-75" /></a></p>
<p>Lately, I have fiddled around with Versant&#8217;s excellent dual-license object database <a href="http://www.db4o.com">db4o</a> 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!</p>
<p>Well, anyhow when you <a href="http://www.db4o.com/DownloadNow.aspx">download db4o</a> (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, <em>there are no tables</em>, but there are objects, so you have to compile them into the application that runs the actual db4o server. There is server <em>code</em> which handles practically everything that is tricky, but it&#8217;s in a dll so you&#8217;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. </p>
<p>I initially thought the VS 2008 designer would set up pretty much everything, but it turned out that things are a little more complicated.</p>
<h2>The Installer</h2>
<p>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 <code>installutil.exe</code>, but using it is a little awkward. Also, you&#8217;d have to deploy it along your application. <a href="http://stackoverflow.com/questions/593454/easiest-language-to-create-a-windows-service">Matt Davis posted a cool guide on stackoverflow</a> and <a href="http://stackoverflow.com/questions/1195478/how-to-make-a-net-windows-service-start-right-after-the-installation/1195621#1195621">some additional information here</a> on how to write a windows service that is esentially able to install itself! </p>
<h3>Unexpected Features</h3>
<p>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 <code>EventLogInstaller</code> and modify it instead of simply adding a new one because the <code>ServiceInstaller</code> will come along with the <code>EventLogInstaller</code> as a child:</p>
<pre class="brush: csharp">
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
    installer.Log = ServiceConfiguration.LogName;
</pre>
<p>We also need to take care of spawning the actual worker thread, but fortunately there is a <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase(VS.80).aspx">comprehensive guide on MSDN</a> explaining this.</p>
<h2>Using the Sample</h2>
<p>First things first: Of course, this is not <em>nearly</em> production-safe code! Also, I did not include the db4o dll&#8217;s because I&#8217;m not sure about the licence terms.<br />
<div id="attachment_74" class="wp-caption alignleft" style="width: 695px"><a href="http://www.emphess.net/wp-content/uploads/2010/01/install.png"><img src="http://www.emphess.net/wp-content/uploads/2010/01/install.png" alt="" title="Install" width="685" height="193" class="size-full wp-image-74" /></a><p class="wp-caption-text">The installer in 'action'. Note the Administrator prompt</p></div></p>
<ol>
<li>Download <a href='http://www.emphess.net/wp-content/uploads/2010/01/db4oSimpleServer.zip'>the sample</a>, extract</li>
<li>Download db4o, install</li>
<li>Copy <code>Db4objects.Db4o.dll</code> and <code>Db4objects.Db4o.CS.dll</code> from the db4o install directory to the samples&#8217; lib directory</li>
<li>Compile</li>
<li>Open an <b>Administrator</b> command prompt and find the sample&#8217;s <code>bin</code> folder. There is no feedback in case anything goes wrong (at best, the program crashes)</li>
<li>Call the install routine by executing <code>emphess.db4oServer.exe -install</code>. This will install the db4o server and the associated event log. The server will not be started automatically.</li>
<li>Head over to <code>services.msc</code> or your EventLog, where you <a href="http://blogs.msdn.com/helloworld/archive/2008/12/11/creating-an-event-log.aspx">should find a new entry!</a></li>
<li>Edit the configuration file, ready to run!</li>
<li>Don&#8217;t use this for anything production&#8230;</li>
</ol>
<h3>The App.Config</h3>
<p>The configuration file looks like this</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
  &lt;appSettings&gt;
    &lt;add key=&quot;UseLocalFile&quot; value=&quot;false&quot; /&gt;
    &lt;!-- &lt;add key=&quot;DatabaseLocalFileName&quot; value=&quot;irrelevant&quot;/&gt; --&gt;
    &lt;add key=&quot;DatabaseHost&quot; value=&quot;localhost&quot; /&gt;
    &lt;add key=&quot;DatabasePort&quot; value=&quot;52354&quot; /&gt;
    &lt;add key=&quot;DatabaseServerFileName&quot; value=&quot;C:/Development/db4oServer/db/server.db4o&quot; /&gt;
    &lt;add key=&quot;DatabaseUser&quot; value=&quot;test&quot; /&gt;
    &lt;add key=&quot;DatabasePassword&quot; value=&quot;test&quot; /&gt;
  &lt;/appSettings&gt;
&lt;/configuration&gt;
</pre>
<p>The <code>UseLocalFile</code> setting is meant for clients so you can easily switch between file-based and network-based configurations. Since <code>db4oServer.Shared</code> contains a small class that holds this configuration data, you can easily reuse the code in a different app, e.g. in the client.</p>
<h3>Todo-List</h3>
<p>There are some things I&#8217;d like to add over the next weeks, so I put the list here to give me some &#8216;pseudo-extrinsic motivation&#8217;&#8230; (yes, I like complicated words)</p>
<ul>
<li>Simple sample client</li>
<li>Out-of-band data for backup, defragmentation commands, etc.</li>
<li>Tests / Benchmarks</li>
</ul>
<p>I hope you have any use for this.</p>
<div class="tweetthis" style="text-align:right;"><p> <a rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=A+Simple+Standalone+Server+for+db4o+as+Windows+Service+http%3A%2F%2Femphess.net%2F%3Fp%3D73" title="Post to Twitter"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter2.png" alt="Post to Twitter" /></a> <a rel="nofollow" class="tt" href="http://delicious.com/post?url=http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/&amp;title=A+Simple+Standalone+Server+for+db4o+as+Windows+Service" title="Post to Delicious"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious.png" alt="Post to Delicious" /></a> <a rel="nofollow" class="tt" href="http://digg.com/submit?url=http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/&amp;title=A+Simple+Standalone+Server+for+db4o+as+Windows+Service" title="Post to Digg"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a rel="nofollow" class="tt" href="http://www.facebook.com/share.php?u=http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/&amp;t=A+Simple+Standalone+Server+for+db4o+as+Windows+Service" title="Post to Facebook"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.emphess.net/2010/01/23/a-simple-standalone-server-for-db4o-as-windows-service/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

