0

For unit testing, I'm running an in-memory server using the System.Web.Http.HttpServer class. It chokes when attempting to instantiate Enterprise Library objects, because Enterprise Library (v5.0.414.0) is configured in the XML configuration file App.config, which isn't being read.

Is it possible to read in the .config file, or am I forced to start a local server (e.g. WcfSvcHost.exe)?

UPDATE:

I'm able to load the configuration thusly (from How to OpenWebConfiguration with physical path?):

var wcfm = new System.Web.Configuration.WebConfigurationFileMap();
var vdm = new System.Web.Configuration.VirtualDirectoryMapping( @"C:\MyPath\", true, "App.config" );
wcfm.VirtualDirectories.Add( "/", vdm );
var cfg = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration( wcfm, "/" );

However, I don't know of any straightforward way to pass that data to the HttpServer object (cfg is a Configuration type, apparently of no relation to the HttpConfiguration type found in HttpServer constructors).

I wanted to avoid implementing an IISExpress solution, but it looks as though I'm forced to go that route in order to support Enterprise Library 5 lameness.

Community
  • 1
  • 1
MiloDC
  • 2,373
  • 1
  • 16
  • 25
  • Which unit testing environment are you using? – Christoph Aug 18 '15 at 21:59
  • MS.VS.TestTools.UnitTesting with VSTest, the standard in Visual Studio. – MiloDC Aug 18 '15 at 22:21
  • Note that I found http://stackoverflow.com/questions/2368748/how-to-openwebconfiguration-with-physical-path, which works for loading the XML file, but presumably a `System.Configuration.Configuration` object is incompatible with the `HttpServer` type. I'm looking for a solution that works for the in-memory `HttpServer`. – MiloDC Aug 18 '15 at 23:04
  • IIS Express might be another option. – Lex Li Aug 19 '15 at 01:59
  • I have Microsoft.VisualStudio.TestTools.UnitTesting tests which test components that rely on settings in the .config file. It's definitely reading the app.config. – Mick Aug 19 '15 at 07:09
  • Are you using the in-memory server from [this](https://www.nuget.org/packages/Microsoft.Owin.Testing/) package? Also have you added the app.config file to your test project? – Anish Patel Aug 19 '15 at 08:37
  • @Mick, as I wrote, I can read App.config but how do apply the configuration to an object of type System.Web.Http.HttpServer? – MiloDC Aug 19 '15 at 20:08
  • @Anish, no, as I wrote, I'm using System.Web.Http.HttpServer. Does Microsoft.Owin.Testing supply a means of in-memory server testing with support for XML configuration files? (And yes, App.config is part of the unit testing project.) – MiloDC Aug 19 '15 at 20:10
  • Perhaps this helps .... http://stackoverflow.com/questions/11851558/testing-route-configuration-in-asp-net-webapi – Mick Aug 20 '15 at 02:37
  • @MiloDC Yes [Microsoft.Owin.Testing](https://www.nuget.org/packages/Microsoft.Owin.Testing/) supplies a means of in-memory server testing and odes support XML configuration files. I use it and read from `App.config` and `Web.config` all of the time. The issue you are having maybe down to your test runner. – Anish Patel Aug 20 '15 at 15:31
  • @Anish, I will give Owin.Testing a serious look ( http://blogs.msdn.com/b/webdev/archive/2013/11/26/unit-testing-owin-applications-using-testserver.aspx ). That might be the way to go, hopefully it will be something that I can implement quickly. Thank you. – MiloDC Aug 20 '15 at 21:50

1 Answers1

0

Turns out, the in-memory HttpServer class works fine; no need for the Owin stuff. I just needed to have references to a few Enterprise Library assemblies in my unit test project as well as my Web API 2 project.

Fusion logging saved the day again. Such a great way to debug obscure library resolution errors. ( http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx )

MiloDC
  • 2,373
  • 1
  • 16
  • 25