装载系统。ServiceModel构成部分使用ConfigurationManager
-
09-06-2019 - |
题
使用。净3.5和周我试着写出一些WCF构在客户应用程序(姓名的客户服务器连接)。
显而易见的方法是使用 ConfigurationManager
载的构成部分,并写出来的数据需要。
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
似乎总是返回null。
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
完美的作品。
该构成部分是存在的应用程序。config但由于某些原因 ConfigurationManager
拒绝装载 system.ServiceModel
部分。
我想要避免手动装xxx.exe。配置文件和使用XPath但是,如果我们要,我会的。似乎只是一点的一个黑客。
任何建议?
解决方案
的 <system.serviceModel>
元是用于一个构成部分 小组, 不一部分。你需要用到 System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
得到整个集团。
其他提示
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 ...
似乎运作良好。
这是我一直在寻找什么感谢@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
不隶属于 StackOverflow