【问题标题】:web.config and app.config confusionweb.config 和 app.config 混淆
【发布时间】:2010-12-02 23:02:48
【问题描述】:

我有一个引用 Web 服务的 DLL。

它放入 app.config 的块是(我已经更改了名称,但你会明白的):

<applicationSettings>
    <DLLName.My.MySettings>
        <setting name="DLLName_WebReferenceName_ASMXName"
            serializeAs="String">
            <value>http://URL/Filename.asmx</value>
        </setting>
    </DLLName.My.MySettings>
</applicationSettings>

我的网站引用了这个 DLL。

问题是,我要在 web.config 中添加什么来覆盖上述设置(或者,我是否只需将 app.config 放在 BIN 目录中)?

我需要能够覆盖生产服务器上 Web 服务的 URL,因为它无法访问 app.config 中指定的 URL(这是我们不会讨论的另一个问题)。

【问题讨论】:

    标签: asp.net web-services web-config app-config


    【解决方案1】:

    在 configSections 中创建一个名为 applicationSettings 的新 sectionGroup 并将您的 app.config 配置粘贴到 web.config 中,如下所示,然后您可以覆盖您的 app.config 设置。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="applicationSettings" 
                    type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="Playground.ConfigurationOverride.DataAccess.Properties.Settings" 
                        type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                        requirePermission="false" />
            </sectionGroup>
        </configSections>
        <applicationSettings>
            <Playground.ConfigurationOverride.DataAccess.Properties.Settings>
                <setting name="MySetting" serializeAs="String">
                    <value>Setting in DataAccess</value>
                </setting>
            </Playground.ConfigurationOverride.DataAccess.Properties.Settings>
        </applicationSettings>
    </configuration>
    

    【讨论】:

      【解决方案2】:

      Asp.Net 配置文件的区别:

      Web.config:

      当您想在 IIS 上托管应用程序时,需要 Web.config。 Web.config 是 IIS 的强制配置文件,用于配置它在 Kestrel 前如何作为反向代理运行。如果你想在 IIS 上托管它,你必须维护一个 web.config。

      AppSetting.json:

      对于其他与 IIS 无关的内容,您可以使用 AppSetting.json。 AppSetting.json 用于 Asp.Net Core 托管。 ASP.NET Core 使用“ASPNETCORE_ENVIRONMENT”环境变量来确定当前环境。默认情况下,如果您在未设置此值的情况下运行应用程序,它将自动默认为生产环境并使用“AppSetting.production.json”文件。当您通过 Visual Studio 进行调试时,它会将环境设置为 Development,因此它使用“AppSetting.json”。请参阅此网站以了解如何在 Windows 上设置托管环境变量。

      App.config:

      App.config 是 .NET 使用的另一个配置文件,主要用于 Windows 窗体、Windows 服务、控制台应用程序和 WPF 应用程序。当您通过控制台应用程序启动 Asp.Net Core 托管时,也会使用 app.config。


      太长了;没读过

      配置文件的选择取决于您为服务选择的托管环境。如果您使用 IIS 来托管您的服务,请使用 Web.config 文件。如果您使用任何其他托管环境,请使用 App.config 文件。 见Configuring Services Using Configuration Files documentation 并查看Configuration in ASP.NET Core.

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 2017-04-07
        • 2010-11-27
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        相关资源
        最近更新 更多