생성된 클라이언트 코드 이해

ServiceModel Metadata Utility Tool (Svcutil.exe) 는 클라이언트 애플리케이션을 빌드하는 데 사용할 클라이언트 애플리케이션 구성 파일과 클라이언트 코드를 생성합니다. 이 항목에서는 표준 서비스 계약 시나리오를 위해 생성된 코드 예제를 간략히 살펴 봅니다. 생성된 코드를 사용하여 클라이언트 애플리케이션을 빌드하는 방법에 관한 자세한 내용은 WCF 클라이언트 개요를 참조하세요.

개요

Visual Studio를 사용하여 프로젝트에 대한 WCF(Windows Communication Foundation) 클라이언트 형식을 생성하는 경우 일반적으로 생성된 클라이언트 코드를 검사할 필요가 없습니다. 동일한 서비스를 수행하는 개발 환경을 사용하지 않는 경우 Svcutil.exe 같은 도구를 사용하여 클라이언트 코드를 생성한 다음 해당 코드를 사용하여 클라이언트 애플리케이션을 개발할 수 있습니다.

Svcutil.exe에는 생성된 형식 정보를 수정하는 많은 옵션이 있으므로 이 항목에서 모든 시나리오에 대해 설명하지는 않습니다. 그러나 다음 표준 작업에는 생성된 코드를 찾는 과정이 포함됩니다.

  • 서비스 계약 인터페이스 식별

  • WCF 클라이언트 클래스 식별

  • 데이터 형식 식별

  • 이중 서비스에 대한 콜백 계약 식별

  • 도우미 서비스 계약 채널 인터페이스 식별

서비스 계약 인터페이스 찾기

서비스 계약을 모델링하는 인터페이스를 찾으려면 System.ServiceModel.ServiceContractAttribute 특성으로 표시된 인터페이스를 검색합니다. 다른 특성이 있고 특성 자체에 명시적 속성이 설정되어 있어 빠른 읽기로 이 특성을 찾기 어려울 수 있습니다. 서비스 계약 인터페이스와 클라이언트 계약 인터페이스는 두 가지 다른 형식입니다. 다음 코드 예제에서는 원래 서비스 계약을 보여 줍니다.

[ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
  [OperationContractAttribute]
  [FaultContractAttribute(typeof(microsoft.wcf.documentation.SampleFault))]
  string SampleMethod(string msg);
}

다음 코드 예제에서는 Svcutil.exe에서 생성된 것과 동일한 서비스 계약을 보여 줍니다.

[System.ServiceModel.ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
    [System.ServiceModel.OperationContractAttribute(
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [System.ServiceModel.FaultContractAttribute(
      typeof(microsoft.wcf.documentation.SampleFault),
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
    )]
    string SampleMethod(string msg);
}

생성된 서비스 계약 인터페이스를 System.ServiceModel.ChannelFactory 클래스와 함께 사용하여 서비스 작업을 호출할 WCF 채널 개체를 만들 수 있습니다. 자세한 내용은 방법: ChannelFactory 사용을 참조하세요.

WCF 클라이언트 클래스 찾기

사용할 서비스 계약을 구현하는 WCF 클라이언트 클래스를 찾으려면 형식 매개 변수가 이전에 찾았으며 해당 인터페이스를 확장하는 서비스 계약 인터페이스인 System.ServiceModel.ClientBase<TChannel>의 확장을 검색합니다. 다음 코드 예제에서는 ClientBase<TChannel> 형식의 ISampleService클래스를 보여 줍니다.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{

    public SampleServiceClient()
    {
    }

    public SampleServiceClient(string endpointConfigurationName)
        :
            base(endpointConfigurationName)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, string remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(binding, remoteAddress)
    {
    }
    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

새 인스턴스를 만들고 구현하는 메서드를 호출하여 이 WCF 클라이언트 클래스를 사용할 수 있습니다. 이러한 메서드는 상호 작용하도록 디자인 및 구성된 서비스 작업을 호출합니다. 자세한 내용은 WCF 클라이언트 개요를 참조하세요.

참고 항목

SvcUtil.exe는 WCF 클라이언트 클래스를 생성할 때 디버거가 WCF 클라이언트 클래스를 단계별로 실행하지 못하도록 하는 DebuggerStepThroughAttribute 를 클라이언트 클래스에 추가합니다.

데이터 형식 찾기

생성된 코드에서 데이터 형식을 찾으려는 경우 가장 기본적인 메커니즘은 계약에 지정된 형식 이름을 식별하고 해당 형식 선언에 대해 코드를 검색하는 것입니다. 예를 들어 다음 계약은 SampleMethod 에서 microsoft.wcf.documentation.SampleFault형식의 SOAP 오류를 반환할 수 있도록 지정합니다.

[System.ServiceModel.OperationContractAttribute(
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
  ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
)]
[System.ServiceModel.FaultContractAttribute(
  typeof(microsoft.wcf.documentation.SampleFault),
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
)]
string SampleMethod(string msg);

SampleFault 를 검색하면 다음 형식 선언을 찾습니다.

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(
  "http://microsoft.wcf.documentation",
  ClrNamespace = "microsoft.wcf.documentation"
)]
namespace microsoft.wcf.documentation
{
    using System.Runtime.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SampleFault : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        private string FaultMessageField;

        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string FaultMessage
        {
            get
            {
                return this.FaultMessageField;
            }
            set
            {
                this.FaultMessageField = value;
            }
        }
    }
}

이 경우 데이터 형식은 클라이언트의 특정 예외인 FaultException<TDetail> 에서 throw되는 세부 유형이며, 여기서 세부 유형 매개 변수는 microsoft.wcf.documentation.SampleFault입니다. 데이터 형식에 대한 자세한 내용은 서비스 계약에서 데이터 전송 지정을 참조하세요. 클라이언트에서 예외를 처리하는 방법에 관한 자세한 내용은 오류 보내기 및 받기를 참조하세요.

이중 서비스에 대한 콜백 계약 찾기

계약 인터페이스가 ServiceContractAttribute.CallbackContract 속성 값을 지정하는 서비스 계약을 찾으면 해당 계약이 이중 계약을 지정합니다. 이중 계약에서는 클라이언트 애플리케이션이 콜백 계약을 구현하는 콜백 클래스를 만들고 해당 클래스의 인스턴스를 서비스와의 통신에 사용되는 System.ServiceModel.DuplexClientBase<TChannel> 또는 System.ServiceModel.DuplexChannelFactory<TChannel> 로 전달해야 합니다. 이중 클라이언트에 대한 자세한 내용은 방법: 이중 계약을 사용하여 서비스 액세스를 참조하세요.

다음 계약에서는 SampleDuplexHelloCallback형식의 콜백 계약을 지정합니다.

[System.ServiceModel.ServiceContractAttribute(
  Namespace="http://microsoft.wcf.documentation",
  ConfigurationName="SampleDuplexHello",
  CallbackContract=typeof(SampleDuplexHelloCallback),
  SessionMode=System.ServiceModel.SessionMode.Required
)]
public interface SampleDuplexHello
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Hello"
    )]
    void Hello(string greeting);
  }
    <System.ServiceModel.OperationContractAttribute(IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Hello")> _
    Sub Hello(ByVal greeting As String)
End Interface 'SampleDuplexHello

해당 콜백 계약을 검색하면 클라이언트 애플리케이션이 구현해야 하는 다음 인터페이스를 찾습니다.

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface SampleDuplexHelloCallback
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Reply"
    )]
    void Reply(string responseToGreeting);
  }
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface SampleDuplexHelloCallback
    <System.ServiceModel.OperationContractAttribute( _
        IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Reply")> _
    Sub Reply(ByVal responseToGreeting As String)
End Interface 'SampleDuplexHelloCallback

서비스 계약 채널 인터페이스 찾기

ChannelFactory 클래스를 서비스 계약 인터페이스와 함께 사용하면 채널을 명시적으로 열고, 닫고, 중단할 수 있도록 System.ServiceModel.IClientChannel 인터페이스로 캐스팅해야 합니다. 더 편리하게 작업할 수 있도록 Svcutil.exe 도구에서는 서비스 계약 인터페이스와 IClientChannel 을 모두 구현하는 도우미 인터페이스도 생성되므로 캐스팅하지 않고도 클라이언트 채널 인프라와 상호 작용할 수 있습니다. 다음 코드에서는 이전의 서비스 계약을 구현하는 도우미 클라이언트 채널에 대한 정의를 보여 줍니다.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel
{
}

참고 항목