【问题标题】:WCF with N-tier architecture具有 N 层架构的 WCF
【发布时间】:2012-08-16 01:17:34
【问题描述】:

我很难让 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; }
        }
}

【问题讨论】:

  • 您的问题是什么?你试过什么??
  • 从 WCF,我试图在 DAL 项目中调用数据库函数,并且 DAL 项目应该从 asp.net 项目中的 web.config 获取数据库连接字符串,但它返回 null 值。 WCF -> DAL -> asp.net (web.config)
  • 您是否尝试将连接字符串写入 DAL 的 web.config?
  • 我在 DAL 项目中没有 web.config,因为它不是 Web 项目,但包含一组库,我不想对现有项目和库进行任何修改。目前,DAL 将在 asp.net 项目中从 web.config 中获取包括数据库连接字符串在内的设置。
  • 看看这个链接也许对你有帮助stackoverflow.com/questions/2548476/…

标签: asp.net wcf


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 2016-11-05
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2023-03-27
    相关资源
    最近更新 更多