I am loading the bindings section from configuration like so
var bingingsSection = BindingsSection.GetSection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
How can I determine if the configuration elements loaded came from the local application config file or if they came from the machine.config?
Use the property bindingsSection.EvaluationContext.IsMachineLevel.
EvaluationContext.IsMachineLevel is also available for ConfigurationElements, such that you can determine it for every single configuration value.
I found the correct answer on my own.
I need to inspect the ElementInformation.Source
property.
Given the following config:
<?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 following 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);
}
}
}
Produces the output:
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 . . .
So as you can see the element defined in my local executable's app.config shows the config path, however, the element I referenced that was not specified in the my local executable's app.config returned a blank string. Presumably because it is the default.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.