![](/img/trans.png)
[英]The configuration section 'system.ServiceModel' cannot be read, missing section declaration
[英]Loading System.ServiceModel configuration section using ConfigurationManager
使用C#.NET 3.5和WCF,我試圖在客戶端應用程序中寫出一些WCF配置(客戶端連接到的服務器的名稱)。
顯而易見的方法是使用ConfigurationManager
加載配置部分並寫出我需要的數據。
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
似乎總是返回null。
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
完美的工作。
配置部分存在於App.config中,但由於某種原因, ConfigurationManager
拒絕加載system.ServiceModel
部分。
我想避免手動加載xxx.exe.config文件並使用XPath,但如果我不得不求助於我。 看起來有點像黑客。
有什么建議?
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 ...
似乎運作良好。
<system.serviceModel>
元素用於配置節組 ,而不是節。 您需要使用System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
來獲取整個組。
這是我正在尋找的感謝@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;
}
GetSectionGroup()不支持任何參數(在框架3.5下)。
而是使用:
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
感謝其他海報,這是我開發的用於獲取命名端點的URI的函數。 它還會創建正在使用的端點列表以及調試時使用的實際配置文件:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.