Web API 2: HttpServer内存服务器和App/Web.config
本文关键字:Web App config 服务器 内存 API HttpServer | 更新日期: 2023-09-27 18:08:31
对于单元测试,我使用System.Web.Http.HttpServer
类运行内存服务器。当尝试实例化Enterprise Library对象时,它会阻塞,因为Enterprise Library (v5.0.414.0)是在XML配置文件App.config
中配置的,而该文件没有被读取。
是否可以在.config
文件中读取,或者我是否被迫启动本地服务器(例如WcfSvcHost.exe
)?
更新:
我能够因此加载配置(从如何使用物理路径打开webconfiguration ?):
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, "/" );
然而,我不知道有任何直接的方法将该数据传递给HttpServer
对象(cfg
是Configuration
类型,显然与HttpServer
构造函数中发现的HttpConfiguration
类型无关)。
我想避免实现IISExpress解决方案,但看起来好像我被迫走那条路,以支持Enterprise Library 5的跛脚。
事实证明,内存中的HttpServer类工作良好;不需要自己的东西。我只需要在我的单元测试项目以及我的Web API 2项目中引用一些企业库程序集。
Fusion日志记录再次挽救了局面。这是调试模糊库解析错误的好方法。(http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx)