ConfigurationManager를 사용하여 System.ServiceModel 구성 섹션 로드
-
09-06-2019 - |
문제
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를 사용하는 것을 피하고 싶지만 꼭 그렇게 해야 한다면 그렇게 할 것입니다.약간의 해킹처럼 보입니다.
어떤 제안이 있으십니까?
해결책
그만큼 <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