使用C#.NET 3.5和WCF,我试图在客户端应用程序(客户端连接到的服务器的名称)中写出一些WCF配置。
显而易见的方法是使用ConfigurationManager加载配置节并写出我需要的数据。
1
| var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel"); |
似乎总是返回null。
1
| var serviceModelSection = ConfigurationManager.GetSection("appSettings"); |
完美运作。
App.config中存在配置节,但由于某些原因ConfigurationManager拒绝加载system.ServiceModel节。
我想避免手动加载xxx.exe.config文件并使用XPath,但是如果需要的话,我会这样做。 似乎有点hack。
有什么建议么?
http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html
1 2 3 4 5 6 7 8 9 10 11 12
| // 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 ... |
似乎运作良好。
元素用于配置节组,而不是节。 您需要使用System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()来获取整个组。
这就是我一直在寻找的东西,感谢@marxidad提供的指针。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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;
} |
GetSectionGroup()不支持任何参数(在框架3.5下)。
而是使用:
1 2
| Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config); |
感谢其他张贴者,这是我开发的用于获取命名端点的URI的功能。 它还会创建正在使用的端点的列表,以及调试时正在使用的实际配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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 |