2008-08-21 22 views
55

Przy użyciu C# .NET 3.5 i WCF, próbuję zapisać niektóre konfiguracje WCF w aplikacji klienckiej (nazwa serwera, z którym klient się łączy).Ładowanie sekcji konfiguracji System.ServiceModel przy użyciu menedżera konfiguracji

Oczywistym sposobem jest użycie ConfigurationManager do załadowania sekcji konfiguracji i zapisania potrzebnych danych.

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

Pojawia się, aby zawsze zwracać wartość null.

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

Działa idealnie.

Sekcja konfiguracji znajduje się w pliku App.config, ale z jakiegoś powodu ConfigurationManager odmawia załadowania sekcji system.ServiceModel.

Chcę uniknąć ręcznego ładowania pliku xxx.exe.config i używania XPath, ale jeśli będę musiał to zrobić, to zrobię to. Po prostu wydaje się trochę hack.

Wszelkie sugestie?

Odpowiedz

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 ... 

Pojawia się działać dobrze.

+1

mylące linia do endpointCollection = clientSection.ElementInformation.Properties [string.Empty] .Value jak ChannelEndpointElementCollection; należy uprościć do clientSection.Endpoints; – joedotnot

14

Tego właśnie szukałem dzięki @marxidad dla wskaźnika.

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() nie obsługuje żadnych parametrów (w ramach 3.5).

Zamiast używać:

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

Dzięki innych plakatów jest to funkcja I opracowany, aby uzyskać URI nazwanego końcowym. Tworzy również listę punktów końcowych w użyciu i które rzeczywisty plik konfiguracyjny był używany podczas debugowania:

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 
Powiązane problemy