Variables Within App.Config/Web.Config

Variables within app.config/web.config

Good question.

I don't think there is. I believe it would have been quite well known if there was an easy way, and I see that Microsoft is creating a mechanism in Visual Studio 2010 for deploying different configuration files for deployment and test.

With that said, however; I have found that you in the ConnectionStrings section have a kind of placeholder called "|DataDirectory|". Maybe you could have a look at what's at work there...

Here's a piece from machine.config showing it:

 <connectionStrings>
<add
name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"
/>
</connectionStrings>

Declare variables in app.config for consuming inside app.config

You can always do something like this in code:

var host = System.Configuration.ConfigurationManager.AppSettings["dbHostAddress"]

var connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["ConnectionString"]
.ConnectionString.Replace("REPLACE_VALUE",host);

You just store the connection string with a placeholder and then pull it into code and swap out the placeholder value with what you want.

Data Source=REPLACE_VALUE;Initial Catalog=Database;
Integrated Security=True;Connect Timeout=15

I would then create a wrapper class around the configuration values so this happens automatically when you access the property in code.

Can we declare variables in the 'app.config' file?

Hmm you can declare your variables like this

<appSettings>
<add key="SmtpServerHost" value="********" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerUserName" value="******" />
<add key="SmtpServerPassword" value="*****" />
</appSettings>

and read like

string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);

using web.config variables within web.config

Here I go answering my own question again :-S

I solved this by writing a NetTcpServiceLocator ...

public interface INetTcpServiceLocator
{
EndpointAddress GetAddress(Type serviceType);
}

... along with a custom config section handler which also implements the above interface and reads in the following config section ...

<services>
<service contract="My.Services.TestService.Contracts.ITestService" address="net.tcp://localhost/TestService" />
</services>

Then I created a proxy for each service ...

public class TestServiceProxy : ITestService
{
public SomeInformation GetSomeInformation(SomeParams @params)
{
using (var factory = new NetTcpServiceFactory<ITestService>())
{
var service = factory.Service;
return service.GetSomeInformation(@params);
}
}
}

My Controller has a dependency on a Service, which has a dependancy on ITestService. All this is glued together with Castle Windsor and by using property dependency injection.

So, my controller calls it's Service, which in turn calls the ITestService (in this case a proxy, which gets it's endpoint from the custom section handler).

The custom section handler (which is also the INetTcpServiceLocator) has a windsor lifestyle of "perWebRequest", so it gets called by the framework and web.config is read into an array in memory. When the service proxy is called, it then just pulls the relevant endpoint based on the contract type.

It's all driven by the type of the contract, so there is no need to have any variables in web.config anymore.

I've gone for a code based solution, as I don't use a build process locally, only when I submit my code to subversion does the build process kick in on our build server.

Environment Variable in app.config appSettings file attribute

"The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located."

https://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx

I'm guessing your scenario works when calling the full path because your MySettingsFile.config is still within your application heirarchy.

How to declare a variable in the app.config file?

There is a <connectionString> section to the app.config file.

<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=sqlserver,1433;Network Library=DBMSSOCN;Initial Catalog=MyDatabase;User ID=xxx;Password=xxxx;" />
</connectionStrings>

For your Host, User ID and Password, you can define these in the <appSettings> section.

What is the recommended way to update the web.config values using release pipeline variables?

The web.config file is XML format type file. So you couldn't use JSON variable substitution option in task to update the value.

Here are the methods to update the value in Web.config file:

1.You can use the XML variable substitution option in the Azure App service deploy task or IIS deploy task.

Sample Image

For more detailed steps, refer to this doc: XML variable substitution

2.You can use PowerShell script to modify the value. But you don't need to add any additional characters in web.config file.

Here is the PowerShell script sample:

$myConnectionString = "test";

$webConfig = '$(build.sourcesdirectory)\Web.config'

Function updateConfig($config)
{
$doc = (Get-Content $config) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $myConnectionString);
$doc.Save($config)
}

updateConfig($webConfig)

3.When you add the mark: #{..}# in web.config file, you can try to use the Replace Token task from Replace Tokens Extension.

Refer to the example in this ticket:How to perform XML Element substitution in web.config using Replace Tokens?

Reading settings from app.config or web.config in .NET

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.



Related Topics



Leave a reply



Submit