简体   繁体   English

从子WCF服务打开父Web应用程序的web.config文件

[英]Open a parent web application's web.config file from child WCF service

Here is the structure of my application. 这是我的应用程序的结构。 I have an asp.net web application running at the root of a website. 我有一个运行在网站根目录的asp.net Web应用程序。 In a child directory I have WebServices\\MyWCFService\\ . 在子目录中,我有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. 最终,我需要做的是打开根应用程序的web.config以获取其连接字符串,以便WCF服务可以连接到此处提供的数据库信息。 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 . 从WCF,我可以获取其自己的web.config ,但是我需要转到网站的根目录才能获取该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"] . 从那里,我应该能够通过调用config.ConnectionStrings.ConnectionStrings["db_name"]来访问连接字符串。

The problem is I can never get past the config declaration where I am getting an ArgumentOfOfRangeException . 问题是我永远也无法通过配置声明,而在该声明中我遇到了ArgumentOfOfRangeException This was expected during testing since the root points to my VS Projects folder at C:\\\\Users\\\\me\\\\Documents\\\\Visual Studio 2012\\\\Projects . 在测试期间,这是预期的,因为root指向C:\\\\Users\\\\me\\\\Documents\\\\Visual Studio 2012\\\\Projects VS Projects文件夹。 So I dropped a test web.config file in there, but I still get the exception. 因此,我在其中放置了一个测试web.config文件,但仍然出现异常。 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. 另外,请记住,在WCF服务中, HttpContext.Current始终为null,因此我无法使用其Request方法以这种方式获取虚拟路径。 Same goes for Server.MapPath . 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 . 我不反对以不同的方式进行操作,只要最终可以打开父应用程序的web.config

I finally found the right ConfigurationManger method to use in this case, where a virtual path is not needed. 我终于找到了在这种情况下不需要虚拟路径的正确ConfigurationManger方法。 Here is the code: 这是代码:

//web config subfolder and filename
const string WEB_CONFIG = "\\Web.config";

//open parent application's web.config file to get connection string to specific database
string svcDir = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
DirectoryInfo rootDir = Directory.GetParent(svcDir).Parent.Parent;
string root = rootDir.FullName;
string webconfigPath = root + WEB_CONFIG;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = webconfigPath;
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

string connectionString = "";
if (configuration.ConnectionStrings.ConnectionStrings["db_name"].ConnectionString.Length > 0)
{
    connectionString = configuration.ConnectionStrings.ConnectionStrings["db_name"].ConnectionString;
}

Works like a charm. 奇迹般有效。

Here is how i ended up doing this: 这就是我最终这样做的方式:

    public string GetConnectionStringValueFromParent(string key)
    {
        string value = string.Empty;
        try
        {
            const string WEB_CONFIG = "\\Web.config";

            string svcDir = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
            DirectoryInfo rootDir = Directory.GetParent(svcDir).Parent;
            string root = rootDir.FullName;
            string webconfigPath = root + WEB_CONFIG;

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = webconfigPath;
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            if (configuration.ConnectionStrings.ConnectionStrings[key].ConnectionString.Length > 0)
            {
                value = configuration.ConnectionStrings.ConnectionStrings[key].ConnectionString;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return value;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM