ConfigurationManager를 사용하여 System.ServiceModel 구성 섹션로드
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
'Nice programing' 카테고리의 다른 글
특정 URL에 대해 스프링 보안을 비활성화하는 방법 (0) | 2020.12.03 |
---|---|
Angular 2에서 앱이 시작될 때 서비스를 실행하는 방법 (0) | 2020.12.03 |
LINQ를 사용하여 목록에서 제공된 인덱스 범위 내의 값을 선택하는 방법 (0) | 2020.12.03 |
IRB에서 스크립트를 어떻게 다시로드 할 수 있습니까? (0) | 2020.12.03 |
캘린더에 항목을 추가하기위한 링크를 생성하려면 어떻게합니까? (0) | 2020.12.03 |