Using RavenDb in a Core 3.x console application

For those using RavenDB in an ASP.Net hosting environment, everything Just Works after a migration to .NET Core 3.x, but for those of us running a generic host as a standalone application it’s a different story. While the Raven team is hard at work migrating the server to 3.1, we are still left with dependency injection interfaces from ASP.Net Core 2.1 when configuring a client. The necessary changes only touch three classes, and only a couple lines of code at that, but it still helps to have a working version at hand when spiking out a new project. You can download a single file with all the necessary classes in it, be sure to add Microsoft.Extensions.Hosting if you haven’t already, and confirm that when you call services.AddRavenDbDocStore() you have that file in the same namespace as the program/setup class so that it ‘wins’ the binding.

For anyone curious as to why this happened, read on. When Microsoft released .Net Core 3.0 they adapted the WebHost startup model into something more generic that allowed developers to reuse application startup code between a web app and a console/service application. This was a fantastic improvement for keeping code consistent and reducing duplication, but it necessitated breaking backwards compatibility (thank you Microsoft for taking that hit. Java, I have 3 words for you – Generic Type-erasure). Where previously an application or framework could work with its hosting environment by taking a dependency on IHostingEnviroment that API is now obsolete, and specific to ASP.Net hosting scenarios anyway. The replacement is in the Nuget Package Microsoft.Extensions.Hosting and named IHostEnvironment, which is actually API compatible with the legacy interface. Any library which needs access to the environment of the host in which it is executing can take a dependency on that IHostEnvironment service, and will get the correct type of host injected at runtime regardless of if it is run in ASP.Net or a console .NET application. Since Raven.Client was built against 2.x it is using that obsolete interface, which is not compatible with the 3.1 hosting bits. There may be other libraries out there with this same problem, and if you encounter any the easiest solution is to grab the source code and change references from IHostingEnvironment to IHostEnvironment.

Leave a Reply

Your email address will not be published. Required fields are marked *