繁体   English   中英

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

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

这是我的应用程序的结构。 我有一个运行在网站根目录的asp.net Web应用程序。 在子目录中,我有WebServices\\MyWCFService\\ 这里概述:

\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

最终,我需要做的是打开根应用程序的web.config以获取其连接字符串,以便WCF服务可以连接到此处提供的数据库信息。 从WCF,我可以获取其自己的web.config ,但是我需要转到网站的根目录才能获取该web.config 我通过执行以下操作获取根目录:

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

现在,要打开配置文件,您需要使用虚拟路径(而不是本地文件系统路径),如下所示:

VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(root, true);
WebConfigurationFileMap wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");

从那里,我应该能够通过调用config.ConnectionStrings.ConnectionStrings["db_name"]来访问连接字符串。

问题是我永远也无法通过配置声明,而在该声明中我遇到了ArgumentOfOfRangeException 在测试期间,这是预期的,因为root指向C:\\\\Users\\\\me\\\\Documents\\\\Visual Studio 2012\\\\Projects VS Projects文件夹。 因此,我在其中放置了一个测试web.config文件,但仍然出现异常。 在生产服务器上,该路径将指向父应用程序的根文件夹,该根文件夹包含配置文件。

另外,请记住,在WCF服务中, HttpContext.Current始终为null,因此我无法使用其Request方法以这种方式获取虚拟路径。 Server.MapPath 有任何想法吗? 我不反对以不同的方式进行操作,只要最终可以打开父应用程序的web.config

我终于找到了在这种情况下不需要虚拟路径的正确ConfigurationManger方法。 这是代码:

//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;
}

奇迹般有效。

这就是我最终这样做的方式:

    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