You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#解决方案中多项目App.config配置文件加载逻辑咨询

How Configuration Files Work in Your C# Multi-Project Solution

Great question—this is a super common point of confusion with .NET solutions that have multiple projects. Let's break down exactly how your solution will pick which config file to use, and why your Class Library is using the Windows Forms App.config right now.

Core Rule: Class Libraries Don't Use Their Own Config Files

First, the key thing to remember: Class Library projects (.dlls) never read their own separate config files. Instead, they use the configuration file of the host process that loads them. The host process is always the executable project you set as the "Startup Project" in your solution.

How This Applies to Your Solution

Let's map this to your specific projects:

  • Windows Forms Project: This is an executable (.exe), so when you set it as the Startup Project and run it, it becomes the host process. Any Class Library it references will read configuration from its App.config (which gets copied to YourWinFormsApp.exe.config in the output folder when you build). That's why your Class Library can access the medFactors connection string you put here.
  • WCF Application Project: This is also a hostable project (it runs as a service, or via IIS/IIS Express). If you set this as the Startup Project, it becomes the host process, and any Class Libraries it references will read from its app.config (copied to YourWcfService.exe.config or web.config if hosted in IIS). Since this config doesn't have your medFactors connection string, the Class Library would fail to find it if the WCF project is the host.

Quick Example of the Flow

If you run the Windows Forms app:

  1. WinForms exe starts, loads its App.config
  2. WinForms references your Class Library, which uses ConfigurationManager.ConnectionStrings["medFactors"]
  3. The Class Library pulls that value directly from the WinForms app's config file

If you run the WCF service:

  1. WCF service starts, loads its app.config
  2. WCF service references the same Class Library
  3. ConfigurationManager.ConnectionStrings["medFactors"] would return null, because that key doesn't exist in the WCF config

Pro Tip for Shared Configs

If you want both the WinForms and WCF projects to use the same connection string (or other settings), you don't have to duplicate the config. You can extract the shared section into a separate file, like SharedConnectionStrings.config, then reference it in both config files:

In your WinForms App.config:

<connectionStrings configSource="SharedConnectionStrings.config" />

In your WCF app.config:

<connectionStrings configSource="SharedConnectionStrings.config" />

Then SharedConnectionStrings.config would contain:

<connectionStrings>
  <add name="medFactors" connectionString="Data Source=JDH8865-1\SQLEXPRESS;Initial Catalog=MedicalFactors;Integrated Security=True"/>
</connectionStrings>

This keeps your config DRY and avoids mismatches between projects.


Your Provided Config Files

Windows Forms App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<connectionStrings>
<add name="medFactors" connectionString="Data Source=JDH8865-1\SQLEXPRESS;Initial Catalog=MedicalFactors;Integrated Security=True"/>
</connectionStrings>
<appSettings>
<!--<add key="RepositoryType" value="FactorsRepositoryService.WCFServiceRepository,FactorsRepositoryService, Version=1.0.0.0, Culture=neutral"/>
<add key="RepositoryType" value="FactorsRepositoryCSV.CSVRepository,FactorsRepositoryCSV, Version=1.0.0.0, Culture=neutral"/>-->
<add key="RepositoryType" value="FactorsRepositorySQL.SQLRepository,FactorsRepositorySQL, Version=1.0.0.0, Culture=neutral"/>
<add key="CSVFileName" value="tbl_Zip-Factors.csv"/>
</appSettings>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPersonService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53959/MedFactorsService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPersonService" contract="WCFMedFactorService.IMedFactorsService" name="BasicHttpBinding_IPersonService" />
</client>
</system.serviceModel>
</configuration>

WCF Application app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMedFactorsService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53959/MedFactorsService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMedFactorsService" contract="WCFMedFactorService.IMedFactorsService" name="BasicHttpBinding_IMedFactorsService" />
</client>
</system.serviceModel>
</configuration>

内容的提问来源于stack exchange,提问作者Dave Hock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:21:36