2008-08-21 11 views
55

C#.NET 3.5とWCFを使用して、クライアントアプリケーション(クライアントが接続しているサーバーの名前)にWCF構成の一部を書き出しようとしています。ConfigurationManagerを使用してSystem.ServiceModel構成セクションをロードする

具体的な方法は、ConfigurationManagerを使用して設定セクションを読み込んで、必要なデータを書き出すことです。

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel"); 

常にnullを返すように表示されます。

var serviceModelSection = ConfigurationManager.GetSection("appSettings"); 

完全に動作します。

構成セクションがApp.configに存在しますが、何らかの理由でConfigurationManagersystem.ServiceModelセクションの読み込みを拒否します。

私はxxx.exe.configファイルを手動で読み込んでXPathを使用しないようにしたいと思いますが、私はそれに頼らなければなりません。ちょっとハックのように思える。

提案がありますか?

答えて

55

http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html

// Automagically find all client endpoints defined in app.config 
ClientSection clientSection = 
    ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; 

ChannelEndpointElementCollection endpointCollection = 
    clientSection.ElementInformation.Properties[string.Empty].Value as  ChannelEndpointElementCollection; 
List<string> endpointNames = new List<string>(); 
foreach (ChannelEndpointElement endpointElement in endpointCollection) 
{ 
    endpointNames.Add(endpointElement.Name); 
} 
// use endpointNames somehow ... 

うまく動作しているように見えます。

+1

.ValueのChannelEndpointElementCollectionとしてendpointCollection = clientSection.ElementInformation.Properties [String.Emptyを】ため紛らわしい線。 は に簡略化する必要があります。clientSection.Endpoints; – joedotnot

14

これは、私がポインタのための@marxidadに感謝していたものです。

public static string GetServerName() 
    { 
     string serverName = "Unknown"; 

     Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig); 
     BindingsSection bindings = serviceModel.Bindings; 

     ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints; 

     for(int i=0; i<endpoints.Count; i++) 
     { 
      ChannelEndpointElement endpointElement = endpoints[i]; 
      if (endpointElement.Contract == "MyContractName") 
      { 
       serverName = endpointElement.Address.Host; 
      } 
     } 

     return serverName; 
    } 
8

GetSectionGroup()は、(フレームワーク3.5の下で)パラメータをサポートしていません。

代わりに使用します。これは私が名前のエンドポイントのURIを取得するために開発された機能である他のポスターに

Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config); 
7

感謝を。また、デバッグ時に実際の設定ファイルが使用されたエンドポイント使用中のリストを作成する:

Private Function GetEndpointAddress(name As String) As String 
    Debug.Print("--- GetEndpointAddress ---") 
    Dim address As String = "Unknown" 
    Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) 
    Debug.Print("app.config: " & appConfig.FilePath) 
    Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig) 
    Dim bindings As BindingsSection = serviceModel.Bindings 
    Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints 
    For i As Integer = 0 To endpoints.Count - 1 
     Dim endpoint As ChannelEndpointElement = endpoints(i) 
     Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString) 
     If endpoint.Name = name Then 
      address = endpoint.Address.ToString 
     End If 
    Next 
    Debug.Print("--- GetEndpointAddress ---") 
    Return address 
End Function 
関連する問題