Skip to content

App.config

Dan Siegel edited this page Apr 28, 2019 · 1 revision

The Mobile.BuildTools now includes support for using an app.config. It's important to note that we do not use the System.Configuration.ConfigurationManager, and instead use a lightweight custom implementation that allows you to initialize custom configurations at runtime which may not follow the typical app.config naming or perform transformations at runtime though this is generally not a good practice.

By default Mobile.BuildTools will look for any file in the root of the head project named app.config or app.*.config. All of those files will be bundled automatically into the native app. If your file has an environment config for the build configuration such as app.debug.config this will perform a transform during build on the bundled app.config.

Using custom configs

In the event that you are using configs that do not reside inside your project directory or do not follow the naming convention you will need to include the configs in your project head.

<ItemGroup>
  <MobileBuildToolsConfigs Include="customConfig.config" />
</ItemGroup>

Alternatively you can simply change the Build Action of the file in Visual Studio to MobileBuildToolsConfigs.

Transformations

A basic app config may look like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="foo" value="my foo" />
    <add key="bar" value="my bar" />
  </appSettings>
  <connectionStrings>
    <add name="test" providerName="my provider" connectionString="my connection string"/>
  </connectionStrings>
</configuration>

A Trasnsformation config may look like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="foo" value="transformed" xdt:Transform="Replace"/>
    <add key="Environment" value="Dev" xdt:Transform="Insert "/>
  </appSettings>
</configuration>

After running the transform from either the automatic build task, at runtime or with the .NET CLI Tool the resulting app.config will look like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="foo" value="transformed" />
    <add key="bar" value="my bar" />
    <add key="Environment" value="Dev" />
  </appSettings>
  <connectionStrings>
    <add name="test" providerName="my provider" connectionString="my connection string"/>
  </connectionStrings>
</configuration>

While the XDT namespace allows you to radically change your app.config. In most cases however you will only need to focus on the xdt:Transform attribute.

  • Replace
  • Insert
  • InsertBefore(XPath expression)
  • InsertAfter(XPath expression)
  • Remove
  • Remove All
Clone this wiki locally