我在使用我的Web应用程序处理WCF项目时遇到了困难。 我在解决方案下有3个项目,包括asp.net项目,DAL,WCF和WCF尝试使用来自DAL的函数,DAL从asp.net项目下的web.config文件中查找数据库连接字符串。但是,每次调用数据库函数时,它都会返回null的连接字符串。好像我无法从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; }
}
}
答案 0 :(得分:0)
System.Configuration.ConfigurationManager类应该处理这种情况。
string connectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
尝试调整您的应用程序以使用它。