Here is the structure of my application. I have an asp.net web application running at the root of a website. In a child directory I have WebServices\MyWCFService\
. Outlined here:
\parentapp\
\parentapp\App_Data\
\parentapp\Web.config
\parentapp\other_parent_stuff
\parentapp\WebServices\
\parentapp\WebServices\MyWCFService\
\parentapp\WebServices\MyWCFService\bin\
\parentapp\WebServices\MyWCFService\bin\MyWCFService.dll
\parentapp\WebServices\MyWCFService\Web.config (WCF's .config)
\parentapp\WebServices\MyWCFService\other_wcf_stuff
Ultimately what I need to do is open the root application's web.config
to get its connection strings so the WCF service can connect to the database info provided there. From the WCF, I can get its own web.config
, but I need to go up to the website's root to get that web.config
. I get the root directory by doing this:
string svcDir = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
DirectoryInfo rootDir = Directory.GetParent(svcDir).Parent.Parent;
string root = rootDir.FullName;
Now apperently to open the config file, you need to use the virtual path (instead of local file system path) like so:
VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(root, true);
WebConfigurationFileMap wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
And from there, I should be able to access the connection string by calling config.ConnectionStrings.ConnectionStrings["db_name"]
.
The problem is I can never get past the config declaration where I am getting an ArgumentOfOfRangeException
. This was expected during testing since the root
points to my VS Projects folder at C:\\Users\\me\\Documents\\Visual Studio 2012\\Projects
. So I dropped a test web.config
file in there, but I still get the exception. On the production servers, the path would point to the parent application's root folder, which contains the config file.
Also, keep in mind that within the WCF Service, HttpContext.Current
is always null so I can't use its Request
method to get the virtual path that way. Same goes for Server.MapPath
. Any ideas? I am not opposed to going about this a different way, so long as ultimately, I can open the parent app's web.config
.
I finally found the right
ConfigurationManger
method to use in this case, where a virtual path is not needed. Here is the code:Works like a charm.