다음을 통해 공유


방법: 서비스 엔드포인트로 메타데이터 가져오기

이 항목에서는 메타데이터를 서비스 엔드포인트 컬렉션으로 가져와서 시작에 정의된 서비스를 사용하는 방법에 대해 설명합니다. 이 항목에서는 서비스에서 메타데이터를 가져온 다음 서비스에 Add 메서드를 호출하는 클라이언트 애플리케이션을 만든 방법을 보여 줍니다.

서비스 엔드포인트로 메타데이터를 가져오려면

  1. EndpointAddress 개체를 선언하고 URI(Uniform Resource Identifier)를 사용하여 서비스의 MEX(메타데이터 교환) 주소에 대해 초기화합니다.

    EndpointAddress mexAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/service/mex");
    
  2. MEX 주소를 전달하여 MetadataExchangeClient를 만들고 GetMetadata를 호출합니다. 그러면 서비스에서 메타데이터가 검색됩니다.

    MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);
    mexClient.ResolveMetadataReferences = true;
    MetadataSet metaSet = mexClient.GetMetadata();
    
  3. 이전에 검색한 메타데이터를 전달하여 WsdlImporter를 만들고 ImportAllContracts를 호출합니다. 그러면 ContractDescription 개체 컬렉션이 생성됩니다. 필요에 따라 ImportAllEndpoints 또는 ImportAllBindings를 호출할 수도 있습니다.

    WsdlImporter importer = new WsdlImporter(metaSet);
    System.Collections.ObjectModel.Collection<ContractDescription> contracts = importer.ImportAllContracts();
    

    참고 항목

    메타데이터를 가져온 후에는 클라이언트 채널을 만들거나 메타데이터를 내보낼 수 없습니다. 이 시점에는 형식 정보를 사용할 수 없기 때문입니다. 실제로 서비스와 상호 작용하거나 메타데이터를 내보내려면 형식 정보가 필요합니다. 형식 정보를 생성하려면 4단계와 5단계에 표시된 코드를 생성해야 합니다. 또는 MetadataResolver 도우미 클래스를 사용할 수도 있습니다. 자세한 내용은 방법: MetadataResolver를 사용하여 동적으로 바인딩 메타데이터 가져오기를 참조하세요.

  4. 각 계약에 대해 형식 정보를 생성합니다.

    ServiceContractGenerator generator = new ServiceContractGenerator();
    foreach (ContractDescription contract in contracts)
    {
        generator.GenerateServiceContractType(contract);
    }
    
    if (generator.Errors.Count != 0)
        throw new Exception("There were errors during code compilation.");
    
  5. 이제 이 정보를 사용할 수 있습니다. 다음 샘플에서는 C# 소스 코드를 생성합니다.

    System.CodeDom.Compiler.CodeGeneratorOptions options = new System.CodeDom.Compiler.CodeGeneratorOptions();
    options.BracingStyle = "C";
    System.CodeDom.Compiler.CodeDomProvider codeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("C#");
    System.CodeDom.Compiler.IndentedTextWriter textWriter = new System.CodeDom.Compiler.IndentedTextWriter(new System.IO.StreamWriter(outputFile));
    codeDomProvider.GenerateCodeFromCompileUnit(generator.TargetCompileUnit, textWriter, options);
    textWriter.Close();
    

참고 항목