简体   繁体   English

关于理解.NET中的ConfigurationManager

[英]On Understanding ConfigurationManager in .NET

I'm trying to understand ConfigurationManager in .NET by practicing it in different scenarios. 我试图通过在不同的场景中练习来理解.NET中的ConfigurationManager。

I have two projects: Project1 and Project2. 我有两个项目:Project1和Project2。 Project2 uses Project1. Project2使用Project1。

My Situation: I have a section(serializedfilename) in my app.config file in project1. 我的情况:我在project1的app.config文件中有一个section(serializedfilename)。 and I have this line of code in a class1 of project1 我在project1的class1中有这行代码

 private static string SerializedConfiguration = ConfigurationManager.AppSettings["SerializedFilename"];

In project2, I call a method in the class1 of project1 and i'm getting an exception. 在project2中,我在project1的class1中调用一个方法,我得到一个异常。

its like .NET doesnt seem to be able to find the settings in app.config file of project1 when called from project2 even though i'v referenced project1 appropriately. 它似乎.NET似乎无法在从project2调用时找到project1的app.config文件中的设置,即使我正确地引用了project1。

However, when i move the section(serializedfilename) to app.config file of project2...everything seems to work well... 但是,当我将section(serializedfilename)移动到project2的app.config文件时...一切似乎都运行良好...

Can someone explain to me what is happening here? 有人可以向我解释这里发生了什么吗? I'm tempted to assume that ConfigurationManager only exist in the context of the client application(in this case project2) and not in the original application(project1 in this case) 我很想假设ConfigurationManager只存在于客户端应用程序的上下文中(在本例中为project2),而不是在原始应用程序中(本例中为project1)

Note: i'm new(1month+ to .NET) 注意:我是新的(1个月+到.NET)

Thanks. 谢谢。

When you add a reference app.Config is not copied. 添加引用app.Config时不会复制。 App.config needs to be in your primary project, in your case project 2. if you add it in project 2 and call it in the referenced object Project 1, you will not get any error as VS will pick it from your primary project. App.config需要在您的主项目中,在您的案例项目中2.如果您在项目2中添加它并在引用的对象Project 1中调用它,您将不会收到任何错误,因为VS将从您的主项目中选择它。

Correct, ConfigurationManager looks to the executing assembly for the app.config or web.config to lookup settings. 正确,ConfigurationManager查找正在执行的程序集以查找app.config或web.config以查找设置。

So if your class library DLL that accesses configuration settings is running under a web site project then the app settings will need to be added to web.config, not the app.config of the class library. 因此,如果访问配置设置的类库DLL在网站项目下运行,则需要将应用程序设置添加到web.config,而不是类库的app.config。

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

相关问题 .NET Core 应用程序中的 ConfigurationManager - ConfigurationManager in a .NET Core application 使用 HttpServer 测试的 ASP.NET WebAPI 中的 ConfigurationManager - ConfigurationManager in ASP.NET WebAPI tested with HttpServer ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗? - Is ConfigurationManager.AppSettings available in .NET Core 2.0? .NET ConfigurationManager app.config混乱 - .NET ConfigurationManager app.config confusion "如何从 .net 6 中的 ConfigurationManager 迁移" - how to migrate away from ConfigurationManager in .net 6 ConfigurationManager - 如何在.NET 3.5中使用连接字符串? - ConfigurationManager - How to use connection strings with .NET 3.5? .NET 4.5 Framework上不存在ConfigurationManager类 - ConfigurationManager Class not exist on .NET 4.5 Framework 使用ConfigurationManager的.net完整框架的.Net Core 2.0应用程序设置 - .Net Core 2.0 appsettings with .net full framework using ConfigurationManager 无法将System.Configuration.ConfigurationManager添加到.NET核心应用程序 - Cannot add System.Configuration.ConfigurationManager to .NET core application 在.NET核心中使用app.config和ConfigurationManager是否可取? - Is using app.config and ConfigurationManager in .NET core advisable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM