跨引用的程序集重复配置

发布于 2024-11-17 07:18:43 字数 435 浏览 2 评论 0原文

假设我们有 Assembly1 和 Assembly2。

Assembly2 是 Assembly1 使用的 C# 类库。

Web 和服务引用在 Asembly2/app.Config 中配置和存储。

此外,EF 连接字符串位于 Assembly2/app.Config 中。

当我在 Assembly1 中使用 Assembly2 时,不使用 Assembly2 配置文件。事实上,在这种情况下,只有 Assembly1 配置看起来可以通过默认方式访问。

因此,我必须将 Assembly2 配置内容复制到 Assembly1 配置中。

这对我的许多项目都有效。

还有其他方法吗?更好的方法?

重复配置数据似乎是错误的。

您有有效的建议或技术吗?

谢谢。

Let's say we have Assembly1 and Assembly2.

Assembly2 is a C# class library used by Assembly1.

Web and Service References are configured and stored in Asembly2/app.Config.

Moreover, the EF connection string(s) are in Assembly2/app.Config.

When I use Assembly2 in Assembly1, the Assembly2 config file is not used. In fact, in that scenario, only the Assembly1 configuration appears accessible through default means.

As a result, I have to copy the Assembly2 config contents into the Assembly1 config.

This has worked for me for many projects.

Is there another way? A better way?

It seems wrong to have repeating configuration data.

Do you have a recommendation or technique that works?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-11-24 07:18:43

您需要将更改应用于入口点 exe 程序集的配置文件。类库程序集 (dll) 配置文件从不使用。它们是由 Visual Studio 制作的,因此您可以根据需要轻松将设置复制到 exe 配置文件。

下面是 exe 程序集的配置文件示例,其中包含类库 ClassLibrary1 中的设置和 exe 程序集 MainAssembly 中的设置。您可以看到两个连接字符串都位于一个 connectionStrings 设置中。但是,如果您需要设置其他设置,除了连接字符串之外,您还需要添加额外的部分。

如果您已经在使用这种技术,那么这是正确的方法。这种技术很灵活。例如,如果一台机器上有多个项目具有相同的连接字符串,则可以在 machine.config 文件中指定连接字符串。如果需要,您还可以覆盖某些项目中的设置。

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <!--This section declaratrion pasted here from dll conifg file -->
      <section name="ClassLibrary1.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

      <!--This section declaratrion was here in the first place -->
      <section name="MainAssembly.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>

    <!--This connection string was here in the first place -->
    <add name="MainAssembly.Properties.Settings.MainAssemblyConnectionString"
         connectionString="MainConnectionStringValue" />

    <!--This connection string pasted here from dll config file -->
    <add name="ClassLibrary1.Properties.Settings.LibraryConnectionString"
         connectionString="LibraryConnectionStringValue"
         providerName="" />
  </connectionStrings>
  <applicationSettings>

    <!--This settings section pasted here from dll config file -->
    <ClassLibrary1.Properties.Settings>
      <setting name="LibrarySetting"
               serializeAs="String">
        <value>LibrarySettingValue</value>
      </setting>
    </ClassLibrary1.Properties.Settings>

    <!--This strings section was here in the first place -->
    <MainAssembly.Properties.Settings>
      <setting name="MainAssemblySetting"
               serializeAs="String">
        <value>MainSettingValue</value>
      </setting>
    </MainAssembly.Properties.Settings>
  </applicationSettings>
</configuration>

You need to apply changes to the config file of entry point exe assembly. Class library assembly (dll) config files are never used. They are made by Visual Studio so you could easily copy the settings to exe config files if needed.

Bellow is example of the config file for exe assembly that has both, settings from class library ClassLibrary1 and settings from the exe assembly MainAssembly. You can see that both connection strings are in one connectionStrings settings. However, if you need to set other settings, beside connection string, you need to add extra section.

If you are already using this technique, this is correct way to go. This technique is flexible. For example if you have more than one project having the same connection strings on one box, you could specify the connection strings in machine.config file. You can also override the settings in some projects if needed.

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <!--This section declaratrion pasted here from dll conifg file -->
      <section name="ClassLibrary1.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

      <!--This section declaratrion was here in the first place -->
      <section name="MainAssembly.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>

    <!--This connection string was here in the first place -->
    <add name="MainAssembly.Properties.Settings.MainAssemblyConnectionString"
         connectionString="MainConnectionStringValue" />

    <!--This connection string pasted here from dll config file -->
    <add name="ClassLibrary1.Properties.Settings.LibraryConnectionString"
         connectionString="LibraryConnectionStringValue"
         providerName="" />
  </connectionStrings>
  <applicationSettings>

    <!--This settings section pasted here from dll config file -->
    <ClassLibrary1.Properties.Settings>
      <setting name="LibrarySetting"
               serializeAs="String">
        <value>LibrarySettingValue</value>
      </setting>
    </ClassLibrary1.Properties.Settings>

    <!--This strings section was here in the first place -->
    <MainAssembly.Properties.Settings>
      <setting name="MainAssemblySetting"
               serializeAs="String">
        <value>MainSettingValue</value>
      </setting>
    </MainAssembly.Properties.Settings>
  </applicationSettings>
</configuration>
记忆里有你的影子 2024-11-24 07:18:43

DLL(或另一个引用的程序集)并不意味着携带它自己的app.config,而是由调用者配置所有内容。所以一切都应该进入exe的app.config中。

例如,考虑一个需要数据库连接字符串的共享数据访问库。该库应该可以从具有不同连接要求的各种应用程序中使用。将连接字符串严格绑定到共享库是行不通的,必须在使用该库的客户端上完成。

可以将影响计算机上运行的所有应用程序的系统范围设置放入 machine.config 文件中,但请谨慎使用该方法,因为它会影响计算机上的所有应用程序。

A DLL (or another referenced assembly) is not meant to carry it's own app.config, but rather have everything configured by the caller. So everything should go into the app.config of the exe.

Consider for example a shared data access library that needs connection strings to the database. The library should be possible to use from a variety of applications with different connection requirements. Having the connection string tied strictly to the shared library wouldn't work, it has to be done at the client using the library.

It is possible to put systemwide settings that affect all applications running on a machine in the machine.config file, but use that approach with caution as it will affect all applications on the machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文