이 샘플에서는 통신할 끝점을 선택하기 위해 서비스에서 메타데이터를 동적으로 검색하는 클라이언트를 구현하는 방법을 보여 줍니다. 이 샘플은 Getting Started 샘플을 기반으로 합니다. 서비스는 두 개의 끝점, 즉 basicHttpBinding 바인딩을 사용하는 기본 주소에 있는 한 끝점과 wsHttpBinding 바인딩을 사용하는 {baseaddress}/secure에 있는 또 다른 보안된 끝점을 노출하도록 수정되었습니다. 끝점 주소와 바인딩을 사용하여 클라이언트를 구성하는 대신에 클라이언트는 MetadataExchangeClient 클래스를 사용하여 서비스에 대한 메타데이터를 동적으로 검색한 다음 WsdlImporter 클래스를 사용하여 메타데이터를 ServiceEndpointCollection으로 가져옵니다.
![]() |
---|
이 샘플의 설치 절차 및 빌드 지침은 이 항목의 끝부분에 나와 있습니다. |
클라이언트 응용 프로그램은 가져온 ServiceEndpointCollection을 사용하여 서비스와 통신하기 위한 클라이언트를 만듭니다. 클라이언트 응용 프로그램은 검색된 각 끝점을 반복하고 ICalculator
계약을 구현하는 각 끝점과 통신합니다. 다음 샘플 코드와 같이 적절한 주소와 바인딩이 검색된 끝점과 함께 제공되므로 클라이언트는 각 끝점과 통신하도록 구성됩니다.
// Create a MetadataExchangeClient for retrieving metadata.
EndpointAddress mexAddress = new EndpointAddress(ConfigurationManager.AppSettings["mexAddress"]);
MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);
// Retrieve the metadata for all endpoints using metadata exchange protocol (mex).
MetadataSet metadataSet = mexClient.GetMetadata();
//Convert the metadata into endpoints.
WsdlImporter importer = new WsdlImporter(metadataSet);
ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
CalculatorClient client = null;
ContractDescription contract = ContractDescription.GetContract(typeof(ICalculator));
// Communicate with each endpoint that supports the ICalculator contract.
foreach (ServiceEndpoint ep in endpoints)
{
if (ep.Contract.Namespace.Equals(contract.Namespace) && ep.Contract.Name.Equals(contract.Name))
{
// Create a client using the endpoint address and binding.
client = new CalculatorClient(ep.Binding, new EndpointAddress(ep.Address.Uri));
Console.WriteLine("Communicate with endpoint: ");
Console.WriteLine(" AddressPath={0}", ep.Address.Uri.PathAndQuery);
Console.WriteLine(" Binding={0}", ep.Binding.Name);
// Call operations.
DoCalculations(client);
//Closing the client gracefully closes the connection and cleans up resources.
client.Close();
}
}
클라이언트 콘솔 창에는 주소 경로와 바인딩 이름을 포함하여 각 끝점에 보내진 작업이 표시됩니다.
샘플을 설치, 빌드 및 실행하려면
Windows Communication Foundation 샘플의 일회 설치 절차를 수행했는지 확인합니다.
C#, C++, Visual Basic .NET 버전의 솔루션을 빌드하려면 Windows Communication Foundation 샘플 빌드의 지침을 따릅니다.
단일 컴퓨터 또는 다중 컴퓨터 구성에서 샘플을 실행하려면 Running the Windows Communication Foundation Samples의 지침을 따릅니다.
![]() |
---|
컴퓨터에 이 샘플이 이미 설치되어 있을 수도 있습니다. 계속하기 전에 다음(기본) 디렉터리를 확인하십시오.
<InstallDrive>:\WF_WCF_Samples
이 디렉터리가 없으면 Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4로 이동하여 WCF(Windows Communication Foundation) 및 WF 샘플을 모두 다운로드하십시오. 이 샘플은 다음 디렉터리에 있습니다.
<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Client\RetrieveMetadata
|