Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config and app.config confusion

I have a DLL that references a web service.

The block it has put into the app.config is (I have changed the names but you'll get the idea):

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

My website references this DLL.

The question is, what do I add to the web.config to override the above setting (alternativly, do I just put the app.config in the BIN directory)?

I need to be able to override the URL for the webservice on the production server because it can't reach the URL specified in the app.config (that is a different issue we wont go into).

like image 542
DomBat Avatar asked Oct 01 '09 10:10

DomBat


People also ask

What is the difference between web config and app config?

In the Web.config case, the configuration system merges the Web.config files in all directories leading up to the application directory into the configuration that gets applied. Web.Config is used for asp.net web projects / web services. App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications

What are the configuration files in a web application?

So, let's start one by one to know more about these configuration files. It is a configuration file, which is used in web application and it can be an ASP.NET project or MVC project. Some project contains multiple web.config file inside the same project but with different folder. They have their unique benefits.

Do I need to create a web config file?

You don’t need to create a web.config file because it is auto generated when you create a new web application. If you want to create a web.config manually you can create it. It is also a special type of configuration file which is basically used with Windows Services, Windows application, Console Apps or it can be WPF application or any others.

How to override an app config configuration in a section?

Create a new sectionGroup in configSections called applicationSettings and paste your app.config configuration into web.config as shown below and then you can override your app.config settings.


2 Answers

Create a new sectionGroup in configSections called applicationSettings and paste your app.config configuration into web.config as shown below and then you can override your app.config settings.

<?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>
like image 160
terjetyl Avatar answered Oct 17 '22 09:10

terjetyl


Differences between Asp.Net config files:

Web.config:

Web.config is needed when you want to host your application on IIS. Web.config is a mandatory config file for IIS to configure how it will behave as a reverse proxy in front of Kestrel. You have to maintain a web.config if you want to host it on IIS.

AppSetting.json:

For everything else that does not concern IIS, you use AppSetting.json. AppSetting.json is used for Asp.Net Core hosting. ASP.NET Core uses the "ASPNETCORE_ENVIRONMENT" environment variable to determine the current environment. By default, if you run your application without setting this value, it will automatically default to the Production environment and uses "AppSetting.production.json" file. When you debug via Visual Studio it sets the environment to Development so it uses "AppSetting.json". See this website to understand how to set the hosting environment variable on Windows.

App.config:

App.config is another configuration file used by .NET which is mainly used for Windows Forms, Windows Services, Console Apps and WPF applications. When you start your Asp.Net Core hosting via console application app.config is also used.


Too Long; Didn't Read

The choice of the configuration file is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file. See Configuring Services Using Configuration Files documentation and also check out Configuration in ASP.NET Core.

like image 45
Alper Ebicoglu Avatar answered Oct 17 '22 11:10

Alper Ebicoglu