0

我很难让 WCF 项目与我的 Web 应用程序一起工作。我在解决方案下有 3 个项目,包括 asp.net 项目、DAL、WCF,并且 WCF 尝试使用 DAL 中的函数,DAL 从 asp.net 项目下的 web.config 文件中查找数据库连接字符串。但是,每次我调用数据库函数时,它都会返回空连接字符串。似乎我无法从 WCF 项目访问 asp.net 项目中的 web.config。理想的场景是使用 asp.net 和 WCF 中的 DAL 而无需修改任何代码。

这个 DAL 库继承 CommonApplication

public abstract class BaseDatabaseApplication : BaseApplication
    {

        protected readonly string myConnection; // connection string to the publisher (master) database




        public BaseDatabaseApplication()
        {
            // all connection strings are configured here, in the base class
            myConnection = CommonApplication.Configuration.ConnectionStrings.ConnectionStrings["myConnection"].ConnectionString;               
            // some function here
        }
}

BaseApplication.cs

public abstract class BaseApplication
    {
        // since its static, there will only be one instance of it.
        private static CommonApplication applications;

        public static CommonApplication Applications
        {
            get { return applications; }
            set { applications = value; }
        }
    }

CommonApplication.cs

public class CommonApplication
    {
        // only want to process the config once.
        private static Configuration configuration;

        public static Configuration Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }
}
4

1 回答 1

0

System.Configuration.ConfigurationManager 类应该处理这种情况。

string connectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

尝试调整您的应用程序以使用它。

于 2012-08-16T02:46:14.163 回答