다음을 통해 공유


Retrieve Metadata

이 샘플에서는 통신할 끝점을 선택하기 위해 서비스에서 메타데이터를 동적으로 검색하는 클라이언트를 구현하는 방법을 보여 줍니다. 이 샘플은 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();
    }
}

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

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

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

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

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

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.