私はそのような構成からバインディングセクションをロードしています
var bingingsSection = BindingsSection.GetSection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
ロードされた構成要素がローカルアプリケーション構成ファイルからのものか、machine.configからのものかをどのように判断できますか?
プロパティbindingsSection.EvaluationContext.IsMachineLevelを使用します。
EvaluationContext.IsMachineLevelは、ConfigurationElementsでも使用できるため、構成値ごとに決定できます。
私は自分で正しい答えを見つけました。
物件を検査する必要がありElementInformation.Source
ます。
次の設定が与えられます:
<?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>
そして、次のアプリケーション
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);
}
}
}
出力を生成します:
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で指定されていない、参照した要素は空白の文字列を返しました。おそらくそれがデフォルトだからです。