{"id":1858,"date":"2011-08-03T20:22:30","date_gmt":"2011-08-03T19:22:30","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1858"},"modified":"2021-07-03T13:29:35","modified_gmt":"2021-07-03T12:29:35","slug":"ravendb-embedded-mode","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2011\/08\/ravendb-embedded-mode\/","title":{"rendered":"RavenDB: Embedded Mode"},"content":{"rendered":"<p>Let&#8217;s take a look at more &#8216;optional&#8217; and supporting features of db4o. One of these is that you can run RavenDB in embedded mode. I personally like embed the database smaller applications. Another big selling point is that this is very nice for testing reasons.<\/p>\n<h2>Embedded Mode<\/h2>\n<p>The first thing we need to do is to reference the required assemblies. The easiest way is to grab the \u2018RavenDB (embedded)\u2019 package via NuGet. Alternatively you can reference the assemblies in \u2018EmbeddedClient\u2019 folder of the RavenDB distribution.<\/p>\n<p>After than we just start up an EmbeddableDocumentStore instance. That\u2019s all, we now can start using the embedded storage.<\/p>\n<script src=\"https:\/\/gist.github.com\/1123399.js?file=UseEmbeddedStorage.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var documentStore = new EmbeddableDocumentStore{DataDirectory = &quot;Data&quot;}.Initialize())\n{\n\t\/\/ now we can use the embedded instance\n\tusing (var session = documentStore.OpenSession())\n\t{\n\t\t\/\/ do work with the session\n\t}\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1863\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inside.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1863\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inside-300x132.png\" alt=\"Raven Inside\" title=\"raven-inside\" class=\"size-medium wp-image-1863\" width=\"300\" height=\"132\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inside-300x132.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inside.png 650w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1863\" class=\"wp-caption-text\">Raven Inside<\/p><\/div>\n<h2>In Memory Instance<\/h2>\n<p>For testing purpose it\u2019s often useful to create in memory database instances, which can be created and thrown away really quickly. That is possible with RavenDB. We just need to set the RunInMemory flag: BOOM, now RavenDB runs in memory.<\/p>\n<script src=\"https:\/\/gist.github.com\/1123399.js?file=InMemory.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var documentStore = new EmbeddableDocumentStore{RunInMemory = true}.Initialize())\n{\n\tusing (var session = documentStore.OpenSession())\n\t{\n\t\t\/\/ Run complex test scenarious\n\t}\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1864\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-memory.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1864\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-memory-300x272.png\" alt=\"Uh, forgot what this blog is about\" title=\"raven-memory\" class=\"size-medium wp-image-1864\" width=\"300\" height=\"272\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-memory-300x272.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-memory.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1864\" class=\"wp-caption-text\">Uh, forgot what this blog is about<\/p><\/div>\n<h2>Embedded HTTP<\/h2>\n<p>When we run an embedded database we cannot inspect that database while the application is running. For that we need to provide access for our administration tools. Well, that\u2019s again a flag away. When we enable the \u2018UseEmbeddedHttpServer\u2019 flag RavenDB will start up a small HTTP server which exposes the regular RavenDB functionality. Then we just can use Raven Studio (the Silverlight app which comes with RavenDB) to connect to our database.<br \/>\n<script src=\"https:\/\/gist.github.com\/1123399.js?file=StartUpHttpServer.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var documentStore = new EmbeddableDocumentStore\n           {\n               DataDirectory = &quot;Data&quot;,\n               UseEmbeddedHttpServer = true\n           }.Initialize())\n{\n\tusing (var session = documentStore.OpenSession())\n\t{\n\t\t\/\/ Run your app\n\t}\n}<\/code><\/pre><\/noscript><br \/>\nAlternatively we can manually start the HTTP server any time when required:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/1123399.js?file=StartHttpServerManually.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">\/\/ Start the HTTP server manually\nvar server = new RavenDbHttpServer(documentStore.Configuration, documentStore.DocumentDatabase);\nserver.Start();<\/code><\/pre><\/noscript><br \/>\nWhen you want to ensure that the application has the rights to open the port you can use this utility to ask for permission. Basically it checks if the application has enough rights to open the port. In case it doesn&#8217;t have the rights it will prompt for it:<\/p>\n<script src=\"https:\/\/gist.github.com\/1123399.js?file=GetRightsForAPort.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);<\/code><\/pre><\/noscript>\n<div id=\"attachment_1865\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inspect.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1865\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inspect-300x195.png\" alt=\"I guess modern heating system do have HTTP interfaces\" title=\"raven-inspect\" class=\"size-medium wp-image-1865\" width=\"300\" height=\"195\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inspect-300x195.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/raven-inspect.png 700w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1865\" class=\"wp-caption-text\">I guess modern heating system do have HTTP interfaces<\/p><\/div>\n<h2>Conclusion<\/h2>\n<p>Now we know how to run RavenDB as embedded database. A wonderful feature, even when we use it only for testing. After this simple feature I hopefully get back to more advanced features next time. =).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s take a look at more &#8216;optional&#8217; and supporting features of db4o. One of these is that you can run RavenDB in embedded mode. I personally like embed the database&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,231],"tags":[21,296],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1858"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1858"}],"version-history":[{"count":9,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1858\/revisions"}],"predecessor-version":[{"id":3883,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1858\/revisions\/3883"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}