Reputation: 4378
Using C# .NET 3.5 and WCF, I'm trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).
The obvious way is to use ConfigurationManager
to load the configuration section and write out the data I need.
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
Appears to always return null.
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
Works perfectly.
The configuration section is present in the App.config but for some reason ConfigurationManager
refuses to load the system.ServiceModel
section.
I want to avoid manually loading the xxx.exe.config file and using XPath but if I have to resort to that I will. Just seems like a bit of a hack.
Any suggestions?
Upvotes: 67
Views: 62202
Reputation: 99957
The <system.serviceModel>
element is for a configuration section group, not a section. You'll need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
to get the whole group.
Upvotes: 51
Reputation: 519
Thanks to the other posters this is the function I developed to get the URI of a named endpoint. It also creates a listing of the endpoints in use and which actual config file was being used when debugging:
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
Upvotes: 7
Reputation: 964
GetSectionGroup() does not support no parameters (under framework 3.5).
Instead use:
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
Upvotes: 10
Reputation: 4378
This is what I was looking for thanks to @marxidad for the pointer.
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;
}
Upvotes: 19
Reputation: 4378
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 ...
Appears to work well.
Upvotes: 65