简体   繁体   English

C#我的测试项目无法从另一个项目的web.config / app.config文件正确加载数据

[英]C# My test project does not load correctly data from the web.config / app.config file of another project

My C# .NET solution includes a Web Service project and a test project. 我的C#.NET解决方案包括一个Web服务项目和一个测试项目。 My DB connection string is set into the WS project's web.config file. 我的数据库连接字符串设置在WS项目的web.config文件中。

I added this web.config file as a link into the test project. 我将此web.config文件添加为测试项目的链接。

When I try to access to the connection string from the test project via: 当我尝试通过以下方式从测试项目访问连接字符串时:

static string pathDataBase = ConfigurationManager.ConnectionStrings["db_asnEntities"].ConnectionString;

The string was not loaded from the web.config link. 该字符串未从web.config链接加载。

Is a test project able to load its conf from a web.config? 测试项目是否可以从web.config加载其conf? Or does it only load it from its App.config (as it also has a test.config)? 还是仅从其App.config中加载它(因为它也具有test.config)? Let me know what I might have missed to have it work. 让我知道我可能错过的工作。

A 一种

The configuration manager will only get values from the current project's config file. 配置管理器将仅从当前项目的配置文件中获取值。 If it is a web project it uses web.config, if not it uses app.config. 如果是Web项目,则使用web.config,否则使用app.config。 I've never tried reading the config from outside the project, I guess you could open it as a file with its full path but that seems like a very bad idea. 我从未尝试过从项目外部读取配置,我想您可以将其作为具有完整路径的文件打开,但这似乎是一个非常糟糕的主意。 I suggest you just enter the connection string in the test project config. 我建议您只在测试项目配置中输入连接字符串。

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

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