How to determine if a configuration section or item is loaded from app.config or machine.config?

I load the bindings section from the configuration as follows

var bingingsSection = BindingsSection.GetSection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));

How to determine if configuration items are loaded from the configuration file of the local application or if they came from the machine.config file?

+3
source share
2 answers

I found the correct answer myself.

I need to check the property ElementInformation.Source.

Given the following configuration:

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding maxReceivedMessageSize="1000000"/>
            </netTcpBinding>
        </bindings>
    </system.serviceModel>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

And the next application

using System;
using System.Configuration;
using System.ServiceModel.Configuration;

namespace ConsoleApplication49
{
    class Program
    {
        static void Main(string[] args)
        {
            var config          = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var bingingsSection = BindingsSection.GetSection(config);

            string netTcpSource    = bingingsSection.NetTcpBinding.ElementInformation.Source;
            string basicHttpSource = bingingsSection.BasicHttpBinding.ElementInformation.Source;

            Console.WriteLine("Net TCP Binding came from \"{0}\"", netTcpSource);
            Console.WriteLine("Basic HTTP Binding came from \"{0}\"", basicHttpSource);
        }
    }
}

Outputs:

Net TCP Binding came from "c:\users\Jim\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\bin\Debug\ConsoleApplication49.exe.Config"
Basic HTTP Binding came from ""
Press any key to continue . . .

, , app.config, , , , app.config, . , .

+1

bindingsSection.EvaluationContext.IsMachineLevel.

EvaluationContext.IsMachineLevel ConfigurationElements, .

+3

All Articles