다음을 통해 공유


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

ServiceModel 메타데이터 유틸리티 도구(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를 추가합니다.

데이터 형식 찾기

생성된 코드에서 데이터 형식을 찾기 위해 가장 기본적인 메커니즘은 계약에 지정된 형식 이름을 식별하고 해당 형식 선언에 대한 코드를 검색하는 것입니다. 예를 들어 다음 계약은 형식SampleMethodmicrosoft.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;
            }
        }
    }
}

이 경우 데이터 형식은 클라이언트의 특정 예외에 의해 throw되는 세부 정보 형식이며 FaultException<TDetail> 여기서 세부 형식 매개 변수는 다음과 같습니다 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
{
}

참고하십시오