System Ładowania.Sekcja Konfiguracja ServiceModel przy użyciu ConfigurationManager
Używając C#. Net 3.5 i WCF, próbuję zapisać część konfiguracji WCF w aplikacji klienckiej (nazwa serwera, z którym Klient się łączy).
Oczywistym sposobem jest użycie ConfigurationManager
do załadowania sekcji konfiguracyjnej i zapisania danych, których potrzebuję.
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
Wydaje się zawsze zwracać null.
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
Działa idealnie.
Sekcja Konfiguracja jest obecna w aplikacji.config ale z jakiegoś powodu ConfigurationManager
odmawia załadowania system.ServiceModel
sekcja.
Chcę uniknąć ręcznego ładowania xxx.exe.plik konfiguracyjny i korzystanie z XPath, ale jeśli będę musiał uciekać się do tego będę. Wygląda na to, że trochę się włamałem.
Jakieś sugestie?5 answers
The <system.serviceModel>
element jest dla sekcji konfiguracyjnej grupy, a nie sekcji. Musisz użyć System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
żeby zdobyć całą grupę.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-02 06:18:11
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 ...
Wygląda na to, że działa dobrze.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-21 10:35:08
Właśnie tego szukałem dzięki @marxidad za wskazówkę.
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-21 10:51:07
GetSectionGroup() nie obsługuje żadnych parametrów (w ramach frameworku 3.5).
Zamiast tego użyj:
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-13 02:19:35
Dzięki innym plakatom jest to funkcja, którą opracowałem, aby uzyskać URI nazwanego punktu końcowego. Tworzy również listę używanych punktów końcowych i tego, który 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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 08:34:13