메타데이터 검색

RetrieveMetadata 샘플은 통신할 엔드포인트를 선택하기 위해 서비스에서 메타데이터를 동적으로 검색하는 클라이언트를 구현하는 방법을 보여 줍니다. 이 샘플은 시작을 기준으로 합니다. 서비스는 두 개의 엔드포인트, 즉 basicHttpBinding 바인딩을 사용하는 기준 주소에 있는 한 엔드포인트와 wsHttpBinding 바인딩을 사용하는 {baseaddress}/secure에 있는 또 다른 보안된 엔드포인트를 노출하도록 수정되었습니다. 엔드포인트 주소와 바인딩을 사용하여 클라이언트를 구성하는 대신에 클라이언트는 MetadataExchangeClient 클래스를 사용하여 서비스에 대한 메타데이터를 동적으로 검색한 다음 ServiceEndpointCollection 클래스를 사용하여 메타데이터를 WsdlImporter으로 가져옵니다.

참고 항목

이 샘플의 설치 절차 및 빌드 지침은 이 항목의 끝부분에 나와 있습니다.

클라이언트 애플리케이션은 가져온 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();
    }
}

클라이언트 콘솔 창에는 주소 경로와 바인딩 이름을 포함하여 각 엔드포인트에 보내진 작업이 표시됩니다.

샘플을 설치, 빌드 및 실행하려면

  1. Windows Communication Foundation 샘플의 일회 설치 절차를 수행했는지 확인합니다.

  2. C#, C++ 또는 Visual Basic .NET 버전의 솔루션을 빌드하려면 Windows Communication Foundation 샘플 빌드의 지침을 따릅니다.

  3. 단일 컴퓨터 또는 다중 컴퓨터 구성에서 샘플을 실행하려면 Windows Communication Foundation 샘플 실행의 지침을 따릅니다.